1doSomething()
2.then(function(result) {
3 return doSomethingElse(result);
4})
5.catch(failureCallback);
1//create a Promise
2var p1 = new Promise(function(resolve, reject) {
3 resolve("Success");
4});
5
6//Execute the body of the promise which call resolve
7//So it execute then, inside then there's a throw
8//that get capture by catch
9p1.then(function(value) {
10 console.log(value); // "Success!"
11 throw "oh, no!";
12}).catch(function(e) {
13 console.log(e); // "oh, no!"
14});
15
1new Promise( (res, rej) => {
2 setTimeout(() => res(1), 1000);
3}).then( (res) => {
4 console.log(res); // 1
5 return res*2
6}).then( (res) => {
7 console.log(res); // 2
8});
9
1const promise2 = doSomething().then(successCallback, failureCallback);
2
1var doSome = new Promise(function(resolve, reject){
2 resolve('I am doing something');
3});
4
5doSome.then(function(value){
6 console.log(value);
7});
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}