美文网首页
手写Promise方法,这回全了

手写Promise方法,这回全了

作者: Shiki_思清 | 来源:发表于2021-10-25 10:00 被阅读0次

    Promise.all

    返回全部成功的数组,有一个失败就返回reject

    Promise.all1 = function(iterators) {
        const currentArr = Array.from(iterators)
        let currentCount = 0
        const len = currentArr.length
        const newPromise = []
    
        return new Promise((resolve, reject) => {
            currentArr.forEach((item) => {
                Promise.resolve(item).then(value => {
                    currentCount++
                    newPromise.push(value)
                    if (currentCount == len) {
                        resolve(newPromise)
                    }
                }).catch(error => reject(error))
            })
        })
    }
    

    Promise.fail

    返回全部失败的数组,有一个成功就返回reject

    Promise.fail = function(iterators) {
        const currentArr = Array.from(iterators)
        let currentCount = 0
        const len = currentArr.length
        const newArr = []
    
        return new Promise((resolve, reject) => {
            currentArr.forEach((item) => {
                Promise.resolve(item).then(value => {
                    reject(value)
                }).catch(reason => {
                    currentCount++
                    newArr.push(reason)
                    if (currentCount == len){
                        resolve(newArr)
                    }
                })
            })
        })
    }
    
    

    Promise.race

    谁快就返回谁,不管resolve或reject

    Promise.race1 = function(iterators) {
        const currentArr = Array.from(iterators)
        return new Promise((resolve, reject) => {
            currentArr.forEach(item => {
                Promise.resolve(item)
                .then(value => resolve(value))
                .catch(reason => reject(reason))
            })
        })
    }
    

    Promise.appSettled

    不管成功失败全部返回下面格式的数据
    [
    { status: 'rejected', reason: 'nonono' },
    { status: 'fulfilled', value: '222222' }
    ]

    Promise.appSettled1 = function(iterators) {
        const currentArr = Array.from(iterators)
        const len = currentArr.length
        let currentCount = 0
    
        const newArr = []
        return new Promise((resolve) => {
            currentArr.forEach(item => {
                Promise.resolve(item).then(value => {
                    currentCount++
                    newArr.push({
                        status: 'fulfilled', value
                    })
                    
                    if (currentCount == len) {
                        resolve(newArr)
                    }
                }).catch(reason => {
                    currentCount++
                    newArr.push({
                        status: 'rejected', reason
                    })
                    if (currentCount == len) {
                        resolve(newArr)
                    }
                })
            })
        })
    }
    

    Promise.any

    如果有一个resolve了,就返回这个resolve
    如果到最后都没有一个resolve, 就返回一个reason集合

    Promise.any1 = function(iterators) {
        const currentArr = Array.from(iterators)
        const len = currentArr.length
        let currentCount = 0
    
        const newArr = []
    
        return new Promise((resolve, reject) => {
            currentArr.forEach(item => {
                Promise.resolve(item)
                    .then(value => resolve(value))
                    .catch(reason => {
                      currentCount++
                      newArr.push(reason)
                      if (currentCount == len) {
                          resolve(newArr)
                      }
                    })
            })
        })
    }
    

    测试代码

    
    // const promise1 = Promise.reject(3)
    // const promise2 = 42
    const promise3 = new Promise((resolve, reject) => {
        setTimeout(reject, 300, 'nonono')
    })
    // const promise4 = Promise.reject('yeyeye')
    const promise5 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('222222')
        }, 200);
    })
    
    Promise.any1([promise3,  promise5])
    .then(values => console.log(values))
    .catch(reason => console.log(reason))
    

    相关文章

      网友评论

          本文标题:手写Promise方法,这回全了

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