美文网首页
Promise 必知必会(十道题)

Promise 必知必会(十道题)

作者: Aniugel | 来源:发表于2019-11-30 16:45 被阅读0次

    来源: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 })
      );
    };
    

    相关文章

      网友评论

          本文标题:Promise 必知必会(十道题)

          本文链接:https://www.haomeiwen.com/subject/adwowctx.html