DEV Community

Discussion on: Async programming basics every JS developer should know

Collapse
 
vonheikemen profile image
Heiker

Nice article. Good job.

One thing that is worth mention is that callback hell can be avoided even if you are "stuck" in an environment that doesn't support promises or async/await. You can always use a function reference instead of an anonymous function.

The game example could look like this:

// ...

var levelThreeReached = function (value) {
  console.log('Level Three reached! New score is ' + value);
}

function levelTwoReached (value) {
  console.log('Level Two reached! New score is ' + value);
  levelThree(value, levelThreeReached);
}

function levelOneReached(value) {
  console.log('Level One reached! New score is ' + value);
  levelTwo(value, levelTwoReached);
}

function startGame() {
  var currentScore = 5;
  console.log('Game Started! Current score is ' + currentScore);
  levelOne(currentScore, levelOneReached);
}