Avinash
Avinash

Reputation: 41

Call one function after another function having a async function in itself

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

Answers (2)

David Fain
David Fain

Reputation: 29

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();
}

Upvotes: 1

Jonas Wilms
Jonas Wilms

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

Related Questions