BigBoy
BigBoy

Reputation: 11

Async function is NOT waiting properly

So i have a main function that is async and it awaits my other function.

main = async () => {
  await this.getResult();
  console.log("end2")
}

so here is the getResult function

getResult() = async () => {
  const result= await fetch(``, this.requestOptions)
  console.log("end1");
  Promise.all(a.map(async b => {
    console.log("Transaction", trabnsaction);
  }))
}

end2 gets printed out first.

Upvotes: 0

Views: 43

Answers (2)

Mina
Mina

Reputation: 17039

You need to return Promise.all, also you need to return a promise from the map method.

Upvotes: 0

Cristian-Florin Calina
Cristian-Florin Calina

Reputation: 1008

You should add the await in the Promise.all as well.

getResult() = async () => {
    const result= await fetch(``, this.requestOptions)

    console.log("end1");

    await Promise.all(a.map(async b => {
        console.log("Transaction", trabnsaction);

    }))
}

Upvotes: 1

Related Questions