Promises vs Async Await – Revisted

In my previous post, I touted how Async Await syntax is less verbose than traditional “then” syntax. While that’s kinda true, it’s kinda not.

Lately, I’ve been having Node.js issues when using AA syntax because of “unhandled promise rejections”. ¬†Thank goodness Node warns you about these, otherwise they’d just disappear in the ether.

This is how to properly throw an error:

NOT this way:

If you throw a string, you won’t see the stack trace (line numbers) when the error is logged. You need to throw an Error object which can take in a string message.

Also, this is the proper way to use async await:

Any usage of await should be wrapped in a try-catch statement. If it’s not, the error will not propagate up the stack, it’ll just remain stuck in the scope of the function evocation that threw the error.

Leave a Reply

Be the First to Comment!