Reputation: 41
How to call one function a()
after another function b()
when b()
contains a async function c()
?
A() {
}
B() {
//do sometihng
c(); //async function
//do something
}
I want to call A()
if B()
including c()
is done executing. But I can not modify function B().
Upvotes: 3
Views: 8449
Reputation: 29
the keyword await
is what you're looking for.
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await : If a Promise is passed to an await expression, it waits for the Promise's resolution and returns the resolved value.
async function c() {
await b();
a();
}
Upvotes: 1
Reputation: 138235
async function b(){
await c();
}
function a(){}
(async function(){
await b();
a();
})()
make b await c, then you can await b and execute a. another way would be:
function b(){
return c();
}
b().then(a);
Upvotes: 3