A promise represents the eventual result of an asynchronous operation
表示一个异步操作的最终结果
一个 Promise 有且只有3种状态:pending,fullfilled,rejected。并且任意时刻都只能处于一种状态
处于 pending 的 Promise
可以转成 fullfilled 或者 rejected 状态
处于 fullfilled 的 Promise
不能转成其他状态
必须有一个 value,并且该 value 不能改变
处于 rejected 的 Promise
不能转成其他状态
必须有一个 reason,并且该 reason 不能改变
这里的不能改变指的是恒等(即可用 ===
判断相等),而不是意味着更深层次的不可变(译者注:盖指当 value 或 reason 不是基本值时,只要求其引用地址相等,但属性值可被更改)。
promise 必须提供一个 then 方法用来访问其当前值、终值和据因
then 方法接受两个参数
promise.then(onFulfilled, onRejected)
参数可选
onFulfilled
和 onRejected
都是可选参数。
onFulfilled
不是函数,其必须被忽略onRejected
不是函数,其必须被忽略