What are Promises in Javascript?
Utilisateur anonyme
Promises are used to handle asynchronous operations in javascript. The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value. A Promise is in one of these states: pending: initial state, neither fulfilled nor rejected. fulfilled: meaning that the operation was completed successfully. rejected: meaning that the operation failed. let promise = new Promise(function (resolve, reject) { const x = "abc"; const y = "abc" if (x === y) { resolve(); } else { reject(); } }); promise. then(function () { console.log('Success, You are a GEEK'); }). catch(function () { console.log('Some error has occurred'); }); The then method is called when the promise is fulfilled. It takes a function as an argument, which will run when the promise is successful. The catch method is called when the promise is rejected. It also takes a function, which will run if there’s an error.