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:
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 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?
For example...if I have:
function func1() { //does something }
function anotherfunc() { //does something }
function func3() { //does something }
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() ;
}
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?
you can't do them in order as each is dependant upon the previous results.
ASKER
All I want is:
etc ...using Async / wait
Like promise.all
run function1 and wait for it to finish...
run function2 and wait for it to finish...
run function3 and wait for it to finish...
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
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.
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.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@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
But that is what async / await does - it does not pause the UI it is just a short hand forpromise.then(resp => console.log('do something'));
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();
@Marek27 - how does your solution differ from the one presented above?