promise的了解
- 依照
Promise/A+
的定义,Promise
有四种状态:pending:
初始状态, 非fulfilled
或rejected.
fulfilled:
成功的操作.rejected:
失败的操作.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(onFulfilled, onRejected)
- 接收两个函数作为参数,一个在
fulfilled
的时候被调用,一个在rejected
的时候被调用,接收参数就是future
,onFulfilled
对应resolve
,onRejected
对应reject
个人博客地址:大家可以看看