DEV Community

Discussion on: What dev topic do you think you should understand, but don't?

 
wuz profile image
Conlin Durbin

You can make each callback function return the function before it - like this:

const topLevelData = funcWithCallback(someVar, () => {
  return anotherFuncWithCallback(someOtherVar, () => {
    return yetAnotherFuncWithCallback(aDifferentVar, () => {
      return "result";
    });
  });
});

And then those functions would need to return their callback:

function yetAnotherFuncWithCallback(data, callback) {
  doSomethingWithData(data);
  return callback();
}

Then you should have it outside of the callbacks!

Thread Thread
 
3leftturns profile image
Andrew T Johnson

Wow thanks for taking the time to explain that. Much clearer now!