静态方法
Promise.all(iterable)
执行所有的
Promise
var promise = Promise.resolve(3);
Promise.all([true, promise]).then(values => {
console.log(values); // [true, 3]
},error=>{
console.log(err)
});
Promise.race(iterable)
执行所有的
Promise
,但是显示最快的一个的结果
Promise.race([new Promise((resolve, reject)=>{
setTimeout(reject, 500, "five");
}), new Promise((resolve, reject)=>{
setTimeout(resolve, 100, "six");
})]).then(value=>{
console.log(value);
},reason=>{
console.log(reason);
});
Promise.reject(reason)
返回
Promise
并且说明原因拒绝
Promise.resolve(value)
返回
Promise
并且给予解决的值,如果value
是Promise
,则会取里面的resolve的值
对象方法
Promise.prototype.then(onFulfilled, onRejected)
onFulfilled
对应resolve
,onRejected
对应reject
。
onFulfilled
的默认值是value=>value
,onRejected
的默认值是reason=>throw reason
2个方法参数可以设置返回值,或者返回一个新的promise
对象,可以链式then
方法
Promise.prototype.catch(onRejected)
类同
Promise.prototype.then(undefined, onRejected)
具体实现
状态机
var PENDING = 0;
var FULFILLED = 1;
var REJECTED = 2;
function Promise(fn) {
// 存储当前的状态 PENDING, FULFILLED or REJECTED
var state = PENDING;
// 存储当前的结果或者错误 FULFILLED or REJECTED
var value = null;
// 存储方法用于`then`或者`done`
var handlers = [];
}
设置回调
function fulfill(result) {
state = FULFILLED;
value = result;
handlers.forEach(handle);
handlers = null;
}
function reject(error) {
state = REJECTED;
value = error;
handlers.forEach(handle);
handlers = null;
}
function handle(handler) {
if (state === PENDING) {
handlers.push(handler);
} else {
if (state === FULFILLED &&
typeof handler.onFulfilled === 'function') {
handler.onFulfilled(value);
}
if (state === REJECTED &&
typeof handler.onRejected === 'function') {
handler.onRejected(value);
}
}
}
this.done = function (onFulfilled, onRejected) {
// 确保我们始终是异步
setTimeout(function () {
handle({
onFulfilled: onFulfilled,
onRejected: onRejected
});
}, 0);
}
this.catch = function (onRejected) {
this.done(undefined,onRejected)
}
处理
// 检查值是不是`Promise`如果是则返回`Promise`的then方法
function getThen(value) {
var t = typeof value;
if (value && (t === 'object' || t === 'function')) {
var then = value.then;
if (typeof then === 'function') {
return then;
}
}
return null;
}
//如果是`Promise`则执行then并且,保证执行一次
function doResolve(fn, onFulfilled, onRejected) {
var done = false;
try {
fn(function (value) {
if (done) return
done = true
onFulfilled(value)
}, function (reason) {
if (done) return
done = true
onRejected(reason)
})
} catch (ex) {
if (done) return
done = true
onRejected(ex)
}
}
function resolve(result) {
try {
var then = getThen(result);
if (then) {
doResolve(then.bind(result), resolve, reject)
return
}
fulfill(result);
} catch (e) {
reject(e);
}
}
doResolve(fn, resolve, reject);
订阅
this.then = function (onFulfilled, onRejected) {
var self = this;
return new Promise(function (resolve, reject) {
return self.done(function (result) {
if (typeof onFulfilled === 'function') {
try {
return resolve(onFulfilled(result));
} catch (ex) {
return reject(ex);
}
} else {
return resolve(result);
}
}, function (error) {
if (typeof onRejected === 'function') {
try {
return resolve(onRejected(error));
} catch (ex) {
return reject(ex);
}
} else {
return reject(error);
}
});
});
}