Reputation: 2731
I need to execute several functions in a queue, but the functions may be sync, async, or even mixed (both) type, such as:
option = 1; // variable
do1() { // sync
console.log(`hello, world`);
}
do2() { // async
this.someService(`example.come`).subscribe( x => {
option = 2;
console.log(x);
}, err => {
option = 3;
console.error(err);
}
}
do3() { // mixed type, sync and async
if (option === 4) {
console.log(`happy day`);
} else {
option = 6;
do2(); // <-- async func
}
}
I hope I can write a very clean code as such:
waitUntilFinish do1();
waitUntilFinish do3();
waitUntilFinish do2();
Is it possible?
Upvotes: 0
Views: 972
Reputation: 378
function f1() {
return new Promise((resolve, reject) => {
console.log('f1');
resolve();
});
}
function f2() {
console.log('f2');
}
f1().then(res => f2());
WITH ASYNC AWAIT
If f1 is asynchronous and return a Promise, use async/await:
async FUNC() {
await f1();
f2();
}
Upvotes: 2