1function doSomething() {
2 return new Promise((resolve, reject) => {
3 console.log("It is done.");
4 // Succeed half of the time.
5 if (Math.random() > .5) {
6 resolve("SUCCESS")
7 } else {
8 reject("FAILURE")
9 }
10 })
11}
12
13const promise = doSomething();
14promise.then(successCallback, failureCallback);
1function myAsyncFunction(url) {
2 return new Promise((resolve, reject) => {
3 const xhr = new XMLHttpRequest();
4 xhr.open("GET", url);
5 xhr.onload = () => resolve(xhr.responseText);
6 xhr.onerror = () => reject(xhr.statusText);
7 xhr.send();
8 });
9}