Skip to main content

You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily:

function x() {
   var promise =return new Promise(function(resolve, reject) => {
     window.setTimeout(function() => {
       resolve('done!');
     });
   });
   return promise;
}

x().then(function(done) => {
  console.log(done); // --> 'done!'
});

With async/await in ES2017 it becomes nicer if inside an async function:

async function() {
  const result = await x();
  console.log(result); // --> 'done!';
}

You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily:

function x() {
   var promise = new Promise(function(resolve, reject) {
     window.setTimeout(function() {
       resolve('done!');
     });
   });
   return promise;
}

x().then(function(done) {
  console.log(done); // --> 'done!'
});

With async/await in ES2017 it becomes nicer if inside an async function:

async function() {
  const result = await x();
  console.log(result); // --> 'done!';
}

You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily:

function x() {
   return new Promise((resolve, reject) => {
     setTimeout(() => {
       resolve('done!');
     });
   });
}

x().then((done) => {
  console.log(done); // --> 'done!'
});

With async/await in ES2017 it becomes nicer if inside an async function:

async function() {
  const result = await x();
  console.log(result); // --> 'done!';
}
Added `async`/`await` example
Source Link
Matt Clarkson
  • 14.4k
  • 10
  • 60
  • 85

You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily:

function x() {
   var promise = new Promise(function(resolve, reject) {
     window.setTimeout(function() {
       resolve('done!');
     });
   });
   return promise;
}

x().then(function(done) {
  console.log(done); // --> 'done!'
});

With async/await in ES2017 it becomes nicer if inside an async function:

async function() {
  const result = await x();
  console.log(result); // --> 'done!';
}

You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily:

function x() {
   var promise = new Promise(function(resolve, reject) {
     window.setTimeout(function() {
       resolve('done!');
     });
   });
   return promise;
}

x().then(function(done) {
  console.log(done); // --> 'done!'
});

You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily:

function x() {
   var promise = new Promise(function(resolve, reject) {
     window.setTimeout(function() {
       resolve('done!');
     });
   });
   return promise;
}

x().then(function(done) {
  console.log(done); // --> 'done!'
});

With async/await in ES2017 it becomes nicer if inside an async function:

async function() {
  const result = await x();
  console.log(result); // --> 'done!';
}
Source Link
Matt Clarkson
  • 14.4k
  • 10
  • 60
  • 85

You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily:

function x() {
   var promise = new Promise(function(resolve, reject) {
     window.setTimeout(function() {
       resolve('done!');
     });
   });
   return promise;
}

x().then(function(done) {
  console.log(done); // --> 'done!'
});