how should i synchronize these 3 promise-based functions?

First, change the functions to return their promises. For example:

function resolveAfter2Seconds() {
  return new Promise(resolve => { // <------ added return statement
    setTimeout(() => {
      resolve('resolved 2');
      console.log('2')
    }, 2000);
  });
}

Then either chain the promises together

resolveAfter2Seconds()
  .then(() => resolveAfter3Seconds())
  .then(() => resolveAfter4Seconds());

Or use async/await syntax:

async function exampleFunction() {
  await resolveAfter2Seconds();
  await resolveAfter3Seconds();
  await resolveAfter4Seconds();
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top