简体   繁体   中英

Using Try/Catch with Promise.all

I have recently been reading about async/await and using try and catch to handle promise rejections, and have been applying it to some of my old code.

I have the following:

async function() {
    try {
        await Promise.all([some functions]);
        doIfNoError();
    } catch (error) {
        console.log(error);
    }

The functions I am passing to Promise.all follow the form:

async function() {
    some code
    if (some condition) {
        return true
    } else {
        throw false
    }
 }

I intend that if any of the functions passed into Promise.all reject, the rejection is displayed. If none of the functions reject, then doIfNoError should fire. However, doIfNoError sometimes fires when it shouldn't, and I am given the error "Unhandled Promise Rejection".

Actually, try/catch does work well with Promise.all() .

Here is a snippet to prove it:

 async function p1() { return 1; } async function boom() { throw new Error('boom'); } // expected output: 'boom', ["value before Promise.all"] async function asyncCall() { let all = ['value before Promise.all']; try { all = await Promise.all([p1(), boom()]); } catch(e) { console.log(e.message); } console.log(JSON.stringify(all)); } asyncCall();

Try using promises to their full potential, which includes a catch block for rejected promises. Note that if doIfNoError also throws an error, it will be caught with catch .

async function() {
    await Promise.all([some promises])
    .then(doIfNoError) // Promise.all resolved
    .catch(console.log) // Promise.all has at least one rejection
}

 promiseAll = async (promises) => { await Promise.all(promises) .then(doIfNoError) // Promise.all resolved .catch(console.log) // Promise.all has at least one rejection } doIfNoError = () => console.log('No errors'); promiseAll([Promise.resolve(), 1, true, () => (false)]); promiseAll([Promise.resolve(), 1, true, Promise.reject('rejected: because reasons'), Promise.resolve()]);

1) throw false – this does not make sense, you should not throw a boolean but an instance of Error instead.

2) catch is only triggered when an error is thrown or the promise is "rejected" (note: reject and throw have the same effect with async-await). In contrast: "Resolving" a promise with a boolean value of false is not interpreted by async-await as an exception. So make sure you throw an error or reject the promise if you want the catch-block to kick in. if you reject , pass the exception as argument to reject, eg ( Promise.reject(new Error("Something went wrong")) .

Apart from what I mentioned above, your code looks fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM