DEV Community

Discussion on: How to Escape Callback Hell with JavaScipt Promises

Collapse
 
kedar9 profile image
Kedar • Edited

Nice article.
Another clean "solution" I'd like to add is to use async/await:

const summerActivities = async () => {
  try {
    const result = await fightTheDemogorgon();
    const seasonsLeft = await rollForDamage(result);
    const finalResult = await closeTheGateIn(seasonsLeft);
    console.log('Hawkins is safe for ' + finalResult + ' more seasons.'));
  } catch (e) {
    failureCallback();
  }
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
sebbdk profile image
Sebastian Vargr

This ^

And' it supported by major browser now. :D
caniuse.com/#feat=async-functions

The poly-fills for async/await can cost a lot of kb depending on how it's transpiled and chunked.

So keep that in mind if speed and legacy is a factor.

Collapse
 
amberjones profile image
AmberJ

Thanks for the addition! 😎