javascript promises then code example
Example 1: js return a promise
function myAsyncFunction(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = () => resolve(xhr.responseText);
xhr.onerror = () => reject(xhr.statusText);
xhr.send();
});
}
Example 2: nodejs promise then example
p.then(onFulfilled, onRejected);
p.then(function(value) {
// run
}, function(reason) {
// fail
});