定义
Promise 是一个对象,用于提供一个未来值,这个值可能是成功值,也可能是失败值。
A promise is an object that may produce a single value some time in the future: either a resolved value, or a reason that it’s not resolved.
三种状态
Pending
initial state, not fulfilled or rejected.
pending 状态就相当于 http 请求正在等待服务器返回结果一样,可能是等待成功,也可能是失败
Fulfilled
the operation completed successfully.
Fulfilled 状态表示 promise 返回了一个成功值,即 resolve 值
Rejected
the operation failed.
Rejected 状态表示 promise 返回了一个失败值,即 reject 值
简单实例
1 | const say = new Promise((resolve, reject) => { |
嵌套 (Chaining)
1 | const greet = new Promise((resolve, reject) => { |
Something Important
错误处理在 promise 中有两种
- Promise.prototype.catch(onRejected)
- Promise.prototype.then(onFulfilled, onRejected)
因为 .then() 总是返回一个 new promise,所以如果在 then 中处理 onRejected,那么在 promise 嵌套时,第二个 .then() 中的代码总会被执行,在这个例子中就会输出 undefined
1 | greet.then( |
个人推荐使用 .catch() 处理 onRejected
多个 Promise
1 | const p1 = new Promise((resolve, reject) => { |
所有的 promise 成功 resolve 后,则 Promise.all 也成功 resolve
只要有一个 promise 被 reject,则 Promise.all 返回的新 promise 也被 reject
Promise.all reject 的值为第一个被 reject 的 promise 的值
结合 async & await (example)
1 | const p1 = new Promise((resolve, reject) => { |