Platinumpt
Platinumpt

Reputation: 33

Promises not running in the expected order

I'm sure I'm missing something simple here, I have a bunch of promises that need to run in order. In this example, function 4 runs before function 3 has completed. All functions called here return a promise.

await self.function1()
  .then(await function () {
    self.function2()
  })
  .then(await function () {
    return self.function3()
  })
  .then(await function () {
    return self.function4()
  })

Upvotes: 1

Views: 77

Answers (1)

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

It should be something like this, You should be using one of async-await or Promise.then

async function test() {
    await self.function1();
    await self.function2();
    const response1 = await self.function3();
    const response2 = await self.function4(response1);
    return response2;
}

OR

function test() {
    return self.function1()
        .then(() => self.function2())
        .then(() => self.function3())
        .then((response1) => self.function4(response1));
}

Upvotes: 1

Related Questions