Promise.all for Rejections and Resolves

By  on  

Promises have been an incredible addition to JavaScript; they save us callback hell, make coding async more maintainable, and and allow us to keep track of multiple async processes at a time.  Promise.all comes to mind, allowing us to react when multiple promises have been resolved.  Unfortunately Promise.all only resolves when all promises are resolved, so if any of the promises fail, the catch is called instead of then:

Promise.all([
    Promise.resolve(1),
    Promise.reject(0)
])
.then(() => { console.log('resolved!'); })
.catch(() => { console.log('failed!') });
// >> failed!

This is a problem if you want the same functionality to be executed regardless of if any promise in the array is rejected.  You could provide the same function to then and catch but that could lead to maintenance issues and occasional "WTF IS THIS?!" comments from other engineers.

So what should we do when we want Promise.all to trigger functionality regardless of any rejections?  Jake Archibald has the answer:

Promise.all(promises.map(p => p.catch(() => undefined)));

Each promise's catch callback returns undefined which allows the promise's failure to be handled as success. To prove it works, consider this snippet:

Promise.all([
    // Resolves
    Promise.resolve(1), 
    // Rejects after 2 seconds
    new Promise((resolve, reject) => setTimeout(() => reject(1), 2000))
].map(p => p.catch(() => undefined))).then(() => console.log('done!'));

// >> done!

Despite the second promise being rejected, the Promise.all then is called! In the future we'll be able to use Promise.prototype.finally to more easily handle success and failure.

Thank you to Jake for this awesome trick!

Recent Features

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    Sexy Album Art with MooTools or jQuery

    The way that album information displays is usually insanely boring. Music is supposed to be fun and moving, right? Luckily MooTools and jQuery allow us to communicate that creativity on the web. The XHTML A few structure DIVs and the album information. The CSS The CSS...

  • By
    The Simple Intro to SVG Animation

    This article serves as a first step toward mastering SVG element animation. Included within are links to key resources for diving deeper, so bookmark this page and refer back to it throughout your journey toward SVG mastery. An SVG element is a special type of DOM element...

Discussion

  1. Fred

    Have you tried RQ, from our famous and beloved Douglas Crockford, a great (and tiny) async library?

    http://www.rq.crockford.com/

  2. J

    This was on StackOverflow about a year ago http://stackoverflow.com/a/36115549/1253312

  3. Matt

    Howdy David,

    Nice find! I was really struggling with this until I found this method of handling errors.

    Rather than just leaving the rejected promise as undefined, if you add in a flag in the reject event, you can catch the error message cleanly and process in the then function, eg:

    Promise.all( arrayOfPromises
    					.map(p => p.catch( (err) => err)) )
    		.then(data => {
    			console.log(data); // you also get err thrown in too, yay!
    		});

    Matt

  4. Alexon da Silva Moreira

    Hello, I have these two query using promise.using, with bluebird, I can only execute one or the other, how do I create a promise all to execute the two function promise and also other function.

    I have:

    **** Promise 1 ****
    
    function fntTotalDeRegistros () {
    promise.using (db, () => {
    db.query ('SELECT COUNT (*) FROM CUSTOMER')
    .then ((rows) => {
    rows.forEach ((row) => {
    qtdRegsARQ = row.registers});
    console.log (qtdRegsARQ)
    })
    .then () => db.close ())
    .catch ((error) => {console.log (error), db.close ()});
    })
    };
    
    **** Promise 2 ****
    
    function fntRegisterCli () {
    promise.using (db, () => {
    db.query ('Select * from DB_ICADIS.CLIENTE ORDER BY cli_codigo')
    .then ((rows) => {
    rows.forEach ((row) => {
    ...........
    });
    })
    .then (() => console.log (regsCLI))
    .then () => db.close ())
    .catch ((error) => {console.log (error), db.close ()});
    })
    };
    
    
    **** function 3 *******
    
    function fntRegistrationFtr () {
    regFTR = func.concatLeft ('FTR', 3, '0');
    qtdRegsARQ = func.concatLeft (qtdRegsARQ, 9, '0');
    resFTR = func.concatRight ('RESERVED', 202, '');
    seqARQ = func.concatRight (seqARQ, 6, '0');
    regsFTR = $ {regFTR} $ {qtdRegsARQ} $ {resFTR} $ {seqARQ} \ n;
    console.log (regsFTR)
    return (regsFTR);
    }
    
  5. Jorge

    The method explained in the article would masquerade errors and unhandled exceptions.
    This post should be updated with the solution in here:
    https://stackoverflow.com/a/32979111/3221029

    Plus Promise.finally is already supported.

  6. Daniel Cohen Gindi

    Using Promise.finally will not help you there, as it does not suppress errors.
    Meaning – the Promise will still throw after you’ve chained it with a finally call.
    Using Promise.catch is still your best bet!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!