DEV Community

JavaScript async and await

Zell Liew 🤗 on April 24, 2019

Asynchronous JavaScript has never been easy. For a while, we used callbacks. Then, we used promises. And now, we have asynchronous functions. Asy...
Collapse
 
mywebstuff_hq profile image
Mayer János

Not completely, as there is one case when it can be beneficial to use return await. Let's say you have three async functions, A,B and C, where A calls B, and B calls C.
Function B calls function C in a try-catch block. If B just returns the promise returned by function C than B puts the burden of error handling on function A as it's catch block will never be called. However if it uses return await C() and C rejects the promise, the erro will be handled by function B's catch block.
I might be making this waaay more complicated than it really is so here is Jake Archibald doing it much better than I did :)
jakearchibald.com/2017/await-vs-re...

Collapse
 
zellwk profile image
Zell Liew 🤗

Way too complicated lol. I just do this:

—-
async function test () {
await a
await b
Return c
}

Test().catch(handleError)

If C errors out, I still catch it with handleError.