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

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)
    })

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理