js promise 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) {
}, function(reason) {
});
Example 3: .then javascript
const promise2 = doSomething().then(successCallback, failureCallback);