依照Promise/A+的定义,Promise有四种状态:
1、 pending:初始状态,非 fulfilled 或 rejected.
2、 fulfilled:成功的操作.
3、 rejected:失败的操作.
4、 settled: Promise 已被 fulfilled 或 rejected,且不是 pending
另外,fulfilled 与 rejected 一起合称 settled
Promise对象用来进行延迟(deferred)和异步(asynchronous)计算
Promise的构造函数
构造一个Promise,最基本的用法如下:
var promise = new Promise(function(resolve, reject) {
if (…) { // succeed
resolve(result);
} else { // fails
reject(Error(errMessage));
}
});
Promise实例拥有then方法(具有then方法的对象,通常被称为thenable)。它的使用 方法如下:
promise. then(onFulfilied, onRejected)
接收两个函数作为参数,一个在fulfilled的时候被调用,一个在rejected的时候被调用,
接收参数就是 future, onFulfilled 对应 resolve, onRejected 对应 reject
Was this helpful?
0 / 0