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().
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().
-
what is in
c()
? – charlietfl Commented Jul 31, 2017 at 16:46
2 Answers
Reset to default 3async 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);
the keyword await
is what you're looking for.
From https://developer.mozilla/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();
}