es6 promise

The arguments to then are optional, and catch(failureCallback) is short for then(null, failureCallback). You might see this expressed with arrow functions instead:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

下面这个代码可以直接控制台执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
new Promise((resolve, reject) => {
 console.log('Initial');
 throw new Error('Something failed');
 // resolve();
})
.then(() => {
 // throw new Error('Something failed'); 
 console.log('Do this');
})
.then(null,
 () => {
 // throw new Error('Something failed'); 
 console.log('Do that');
 }
 )
.then(() => {
 console.log('Do this, no matter what happened before');
});

上面这个代码等同于

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
new Promise((resolve, reject) => {
 console.log('Initial');
 throw new Error('Something failed');
 // resolve();
})
.then(() => {
 // throw new Error('Something failed'); 
 console.log('Do this');
})
.catch(() => {
 console.error('Do that');
})
.then(() => {
 console.log('Do this, no matter what happened before');
});

When you return something from a then() callback, it’s a bit magic. If you return a value, the next then() is called with that value. However, if you return something promise-like, the next then() waits on it, and is only called when that promise settles (succeeds/fails).

1
2
3
4
5
6
Promise.resolve('foo').
 then(() => {return "bar"}).
 then((v) => console.log(v))
Promise.resolve('foo').
 then(() => {return Promise.resolve('bar')}).
 then((v) => console.log(v))

https://stackoverflow.com/questions/34094806/return-from-a-promise-then