2

Even after reading several answers to similar questions (e.g. this and that) I unfortunately still do not understand, why this code does not await the promises and hence logs ['check2'] last after the other checkpoints.

This is a minimal example using code from this guide. In the original code I need to fetch some Information from different sources before my express server can start listening.

console.log("check1");

const resolveInTwoSeconds = () => {
  return new Promise((resolve) => {
    setTimeout(() => resolve("check2"), 2000);
  })
};
async function test() {
  const asyncFunctions = [
    resolveInTwoSeconds()
  ];
  const results = await Promise.all(asyncFunctions);
  console.log(results);

}
(async() => await test())();
console.log("check3");

EDIT: Imagine "check3" to be a lot of code that is depended on the sideeffects of test(). Thus I want it to run after check2 was printed. However I am using await here so I do not have to change or move "check3".

Robin
  • 177
  • 1
  • 8

2 Answers2

2

This line of code declares an async function and executes it:

(async() => await test())();

So far nothing waits for its result and the execution goes on to console.log("check3").

You have to explicitly wait for it:

await (async () => await test())();

Now, this won't work yet, because the top-level function is not async. Whenever you need to call await, you have to make sure it's called inside an async function. One way to do this would be to wrap everything inside another async function:

(async () => {
  console.log("check1");

  const resolveInTwoSeconds = () => {
    return new Promise((resolve) => {
      setTimeout(() => resolve("check2"), 2000);
    })
  };
  async function test() {
    const asyncFunctions = [
      resolveInTwoSeconds()
    ];
    const results = await Promise.all(asyncFunctions);
    console.log(results);

  }
  await (async ()=> await test())();
  console.log("check3");
})()

Otherwise, move your check3 into an async function you already have, as others suggested.

shkaper
  • 4,689
  • 1
  • 22
  • 35
  • 1
    Ok, thanks all. Meaning it's not possible without touching "check 3" (either moving it into the wrapper function or using a callback. – Robin Mar 05 '20 at 14:03
1

This should do what you want. you need the console.log to be inside the async function.

console.log("check1");

const resolveInTwoSeconds = () => {
  return new Promise((resolve) => {
    setTimeout(() => resolve("check2"), 2000);
  })
};
async function test() {
  const asyncFunctions = [
    resolveInTwoSeconds()
  ];
  const results = await Promise.all(asyncFunctions);
  console.log(results);

}
(async() =>{ 
    await test();
    console.log("check3");
})();
Ahmed Tounsi
  • 1,482
  • 1
  • 14
  • 24
  • move the code that supposed to be check3 into a function and call it after test, that's the only solution I can think of @Robin. – Ahmed Tounsi Mar 05 '20 at 13:53