DEV Community

Discussion on: How to Escape Callback Hell with JavaScipt Promises

Collapse
 
vonheikemen profile image
Heiker • Edited

When there is no way to avoid callback style functions, named functions can help ease the pain.

function fight(result) {
  rollForDamage(result, keepFighting, failureCallback);
}

function keepFighting(seasonsLeft) {
  closeTheGate(seasonsLeft, finishHim, failureCallback);
}

function finishHim(result) {
  console.log('Hawkins is safe for ' + result + ' more seasons.');
}

fightTheDemogorgon(fight);

Also, if you are using node you can use the promisify utility to convert those to promise based functions.

Collapse
 
amberjones profile image
AmberJ

Definitely! Love some bluebird and util. Thanks for commenting Heiker!