Skip to content Skip to sidebar Skip to footer

Managing Sync And Async Functions In An Algorithm Call Stack

TLDR: must I use async and await through a complicated call stack if most functions are not actually async? Are there alternative programming patterns? Context This question is pr

Solution 1:

If you don't want to make the function async and propagate this up the chain, use .then(). You'll need to duplicate the following code inside .then(); you can simplify this by putting it in its own function.

function maybeRepeat() {
  if (stop_condition) {
    finish();
  } else {
    calcStep1();
  }
}

function calcStep3() {
  // do more calculations
  pushToResults();

  if (some_condition) {
    getData().then(maybeRepeat);
  } else {
    maybeRepeat()
  }
}

Post a Comment for "Managing Sync And Async Functions In An Algorithm Call Stack"