DEV Community

Discussion on: The dangers of async/await

Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen

The most overlooked caveat of async-await is that we have to wrap it in a try-catch block to handle errors by catching rejected promises.

Collapse
 
thibmaek profile image
Thibault Maekelbergh

I don't see why this would be considered a caveat? Thenable promises also need to explicitly declare a Promise#catch() to handle errors? What makes it different from wrapping async/await in try-catch?

Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen

I mean it's something to be aware of and it's often overlooked and not mentioned at all in articles discussing and guiding on async-await.

The difference between this and promises is that catch callbacks can be put specifically in any place in a promise chain. Of course, we can also do this with try-catch around a single statement, but that gets ugly pretty fast, especially if we want to keep the same variable scope between related try-catch blocks.