0

Is there a way in Javascript to wait for the completion of another function and it's callback and essentially "pause" the completion of the current function until that time?

Example:

var doSomething = function(callback) {
  doSomethingElseAsynchronously(doSomething);
  /* Wait until doSomethingElseAsynchronously */
  /* is finished and it's callback has completed */
  return callback();
};
FatalKeystroke
  • 2,882
  • 7
  • 23
  • 35

1 Answers1

0

What you're thinking of is a current proposal in ES7 know as async functions. Their syntax looks like this:

async function doSomething() {
    let result = await doThingAsync(); // doThingAsync returns a Promise
    // ... Do things ...
    return result[0]
}

async functions return a Promise which resolve with the eventual return value of the function.

Note that async functions have little to no support, and should you choose to use them, you must first transpile your code to ES6 or ES5 using something like Babel

bren
  • 4,176
  • 3
  • 28
  • 43