handling promise in javascript code example
Example 1: javascript promises
var promise = new Promise(function(resolve, reject) {
// do some long running async thing…
if (/* everything turned out fine */) {
resolve("Stuff worked!");
}
else {
reject(Error("It broke"));
}
});
//usage
promise.then(
function(result) { /* handle a successful result */ },
function(error) { /* handle an error */ }
);
Example 2: making promises in js
getData()
.then(data => console.log(data))
.catch(error => console.log(error));