Promise.all方法中如果要在单个Promise中捕捉回调该怎么写呢?

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

function b (p: number): Promise {
    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)
    })

你可能感兴趣的:(typescript)