关于typescript:Promiseall方法中如果要在单个Promise中捕捉回调该怎么写呢

3次阅读

共计 428 个字符,预计需要花费 2 分钟才能阅读完成。

function a (p: number): Promise<number> {return new Promise((res, rej) => {if (p == 2) rej(100)
        setTimeout(() => {res(p)
        }, 2000)
    })
}

function b (p: number): Promise<number> {return new Promise((res, rej) => {if (p == 3) rej(200)
        setTimeout(() => {res(p)
        }, 2000)
    })
}

// 这里的 catch 也能够通过 map 函数对立加
Promise.all([a(1).catch(err => {console.log("出错了 1111", err)
    }), b(3).catch(err => {console.log("出错了 2222", err)
    })])
    .then(res => {console.log("后果是", res)
    })
    .catch(err => {console.log("出错了", err)
    })
正文完
 0