Swizec Teller - a geek with a hatswizec.com

    A promises gotcha that will catch you out

    You think you're a JavaScript expert and then you find this all over your codebase.

    Happened to us on Friday. You shoulda seen the look of defeat on my coworker's face. Looked like he was ready to give up software and become a goat herder in the Andes πŸ˜‚

    goat_herder giphy

    So what happened?

    He's performing a networking audit of our codebase. A large app we've built over the years. I built the first version in Backbone, then a team rewrote with Vue, then we all kind of contributed here and there.

    And there's been issues. Bugs so frequent users gave up reporting them. They just refresh and move on.

    Well, our networking layer leaks errors like a rusty old bucket you find in the back of the shed. API throws an error, app flinches, shrugs it off, and moves on.

    Users get undefined behavior galore. App thinks there's data. There isn't data.

    2i7MXrB

    "But how", you might ask, "Doesn't modern JavaScript make error handling easy as shit?"

    It does! Except when it doesn't.

    Look at this code:

    Click through for source
    Click through for source

    A mock makeRequest method that always rejects its promise. An async/await version would throw instead.

    Like this

    Click through for source
    Click through for source

    Same effect. Promises make the mistake I'm talking about more apparent. You'll see why.

    Okay so you've got this function that sometimes returns an error. You try to be a good citizen, which means handling errors instead of blowing up your app.

    Click through for source
    Click through for source

    You have a getFirstItem method that calls the API and returns an item from its response. If there's an error, it says there was an error.

    All good, right? Job well done! You're doing error handling and everything. So good.

    Now look at this code, what does it print?

    Click through for source
    Click through for source

    Think about it, I'll send you the answer in 30 minutes πŸ˜›

    PS: if you're not that familiar with promises, here's an old video I made in 2017 – a 2min explanation.

    Click through for source
    Click through for source


    Click through for source
    Click through for source

    Did you figure it out {{subscriber.first_name}}? My team got it wrong. Even folks with decades of JavaScript experience.

    BYgtzPO

    That's right, it prints undefined.

    o.O

    You get an error from getFirstItem. But then the error is handled!. The rest of your code carries on in the .then chain like nothing happened.

    And that's how you build a leaky bucket without realizing it. Multiply that across months and years and many developers and well ... πŸ˜…

    The fix is to always re-throw errors. Unless you have a damn good reason why it needs to be eated.

    Click through for source
    Click through for source

    With that tiny modification the code works as expected.

    d89bP3j

    thumbs_up giphy

    Yes the same rule applies to async/await code. You're eating errors unless you explicitly re-throw.

    Now go through your project looking for this mistake and try not to cry. πŸ˜›

    Good luck and happy Monday

    ❀️, ~Swizec

    Did you enjoy this article?

    Published on December 16th, 2019 in Front End, Technical

    Learned something new?
    Want to become an expert?

    Here's how it works πŸ‘‡

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters πŸ’Œ on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. πŸ‘Œ"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer?Β I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own? Β Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first πŸ’° on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax?Β Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❀️

    Created bySwizecwith ❀️