美文网首页
promise ALL

promise ALL

作者: 织雪纱奈 | 来源:发表于2019-08-19 15:19 被阅读0次
    promise.all = function(promises){
      return new Promise((resolve,reject)=>{
          let result = []
          let cnt = 0
          let len = promises.length
          for (let i=0; i<len;i++) {
            Promise.resolve(promises[i]).then((res)=>{
              cnt++
              result[i] = res
              if(cnt === len) {
                resolve(result)
              }
            },(err)=>{
              reject(err)
            })
          }
      })
    } 
    
    promise.race = function (promises) {
      return new Promise((resolve, reject) => {
        for (let i = 0; i < promises.length; ++i) {
          promises[i].then(resolve, reject)
        }
      })
    }
    
    
    

    相关文章

      网友评论

          本文标题:promise ALL

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