0
Q:

how to wait for a function to finish in javascript

function firstFunction(_callback){
    // do some asynchronous work
    // and when the asynchronous stuff is complete
    _callback();    
}

function secondFunction(){
    // call first function and pass in a callback function which
    // first function runs when it has completed
    firstFunction(function() {
        console.log('huzzah, I\'m done!');
    });    
}
2
async function processArray(array) {
  // map array to promises
  const promises = array.map(delayedLog);
  // wait until all promises are resolved
  await Promise.all(promises);
  console.log('Done!');
}
0
function doFirst() {
  return new Promise(function(resolve, reject) {
    //do a thing
      if (){
          return reject(value);
  	  }
      resolve(value);
    });
  });
}

async function doSecond() {
    var outputValue = await doFirst();
    console.log(outputValue);
}
0
console.log('1')

setTimeout(function afterTwoSeconds() {
  console.log('2')
}, 2000)

console.log('3')
-2

New to Communities?

Join the community