Link to home
Start Free TrialLog in
Avatar of error2013
error2013

asked on

Async/Await run a number of functions in it's given order

If I need to run an unknown number of functions in a specific order what is the best way to do this using Async await?

For example...if I have:

function func1() { //does something }
function anotherfunc() { //does something }
function func3() { //does something }

Open in new window


The number of functions above could be 3 or 10.

Then using async away, I need to call them in order:

So I have another function called process:

function process() {
  func3(); 
 func1();
 anotherfunc() ;
}

Open in new window


function process needs to run the functions in the order that they are placed and each has to finish before the other one starts.

How can I do this?
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

you can't do them in order as each is dependant upon the previous results.
Avatar of error2013
error2013

ASKER

All I want is:

run function1 and wait for it to finish...
run function2 and wait for it to finish...
run function3 and wait for it to finish...

Open in new window


etc ...using Async / wait

Like promise.all
then why use async /wait if they need to be run one after the other
you can use 1 async/await and do all 3 allowing UI to continue
1) You can start any number of functions in a given order. That's up to you. However you start your functions, just setup whatever order you like.

2) You can only start functions in a given order. When running async, you'll have no control over when functions complete. That's the nature of async, so starting them in a particular order... seems... doesn't seem to be much use, other than for initial debugging maybe.
So async / await without promises cannot run functions in an order like Promise.All does?
Entirely different processes and rules of behaviour
With async... async means 100% async...

You can arrange to start processes... however you start them, in any order.

They they run on their own as a herd. Each function will end, when it ends, with no ordering... unless you somehow inject yields into the functions via some mechanism.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@David
then why use async /wait if they need to be run one after the other
you can use 1 async/await and do all 3 allowing UI to continue 

Open in new window

But that is what async / await does - it does not pause the UI it is just a short hand for

promise.then(resp => console.log('do something'));

Open in new window

It will not stop the UI - all it does is ensures that the line after the await line runs after the await call is finished but normal execution continues.
this might clear it up a bit I hope

function oneAsync() {
  const TIMEOUT_MS = 3000;
  
  return new Promise((resolve, reject) => { 
    setTimeout(() => {
      const result = 'one';
      console.log('doing function one => ', result);

      if (result) {
        return resolve(result);
      } else {
        return reject(new Error(`Function ONE didn't go as planned ...`));
      }
    }, TIMEOUT_MS);
    
  });
}

function twoAsync(resultFormPrev) {
  const TIMEOUT_MS = 1000;
  
  return new Promise((resolve, reject) => { 
    setTimeout(() => {
      const result = `from Two => ${resultFormPrev}`;
      console.log('doing function two => ', result);

      if (result) {
        return resolve(result);
      } else {
        return reject(new Error(`Function TWO didn't go as planned ...`));
      }
    }, TIMEOUT_MS);
  });
}

async function runThemAll() {
 const resultOne = await oneAsync();
 const resultTwo = await twoAsync(resultOne);
}

runThemAll();

Open in new window

@Marek27 - how does your solution differ from the one presented above?