来源:https://juejin.im/post/5a04066351882517c416715d
设计并实现 Promise.race() 链接
const _race = (p)=>{
return new Promise((resolve, reject)=>{
p.forEach((item)=>{
Promise.resolve(item).then(resolve, reject)
})
})
}
模拟实现一个 Promise.finally 链接
Promise.prototype.finally = function (callback) {
let P = this.constructor;
return this.then(
value => P.resolve(callback()).then(() => value),
reason => P.resolve(callback()).then(() => { throw reason })
);
};
网友评论