手写Promise方法,这回全了

2021-10-25  本文已影响0人  Shiki_思清

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))
上一篇下一篇

猜你喜欢

热点阅读