3

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().

Avinash
  • 41
  • 1
  • 5

2 Answers2

3
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);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

the keyword awaitis 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();
}
David Fain
  • 29
  • 3