javascript make a function promise code example
Example 1: .promise()
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"));
}
});
Example 2: making promises in js
getData()
.then(data => console.log(data))
.catch(error => console.log(error));