0

Consider a scenario such as this :

function method1(): Promise<string> {
  return new Promise((resolve, reject) => {
    // do something
    const response = true;
    setTimeout(() => {
      if (response) {
        resolve("success");
      } else {
        reject("error");
      }
    }, 5000);
  });
}

async function method2(): Promise<string> {
  const result = await method1();
  // do some other processing and return a different result
  const result2 = result + "1";
  return result2;
}

function method3(): void {
  console.log("test 1");
  const result = method2();
  console.log("test 2");
}

method3();

I am not sure why method2() would not wait for the result as it contains an await. If I use await for method2() call in method3(), it works :

async function method3(): Promise<string> {
  console.log("test 1");
  const result = await method2();
  console.log("test 2");
}

I can't really wrap my head around how the await/async work even after reading so many blog posts, Mozilla documentation and stackoverflow answers.

One of the comments said "you can't escape asynchrony". And further explained that as any async function would be returning a promise, have to await them all up the function ladder.

Hope someone can clarify this for me. Thank you.

R. Richards
  • 24,603
  • 10
  • 64
  • 64
  • You're not using `await` to call the `async` function. – Pointy Jun 25 '21 at 00:02
  • yes I get what to do to "fix" it. I don't understand the reason behind it. For eg is it a rule that you have to await all async functions ? `method1()` returns a promise, which is awaited by `method2()`. So I'd assume when you call `method2()`, it will automatically wait for `method1()` –  Jun 25 '21 at 00:08

2 Answers2

0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

every async function return a promise and that will go "up the chain" up to the first call.

there are some libs that aim to make synchronous calls from promises if you really want.

but the thing is: that's just how javascript works

Noriller
  • 342
  • 1
  • 4
  • 12
  • thank you for the spot on answer , and after reading this https://stackoverflow.com/questions/56895695/why-do-i-need-to-await-an-async-function-when-it-is-not-supposedly-returning-a-p?rq=1 I understand better now I will delete this question, thanks for your time mate –  Jun 25 '21 at 00:17
0
  • Mark async functions as async
  • Functions that await async functions are themselves async
  • Await async functions when you call them (if you need the result to continue execution), except at the top level, where you can use the equivalent then syntax

async function method1() {
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve(1), 5000);
  });
}

async function method2() {
  console.log("starting method 2");
  const result = await method1();
  console.log("finished method 2");
  return result + 1
}

async function method3() {
  console.log("starting method 3");
  const result = await method2();
  console.log("finished method 3");
  return result + 1
}

method3().then(result => console.log(result))
danh
  • 62,181
  • 10
  • 95
  • 136