0

promises are executed only if you call .then on them or this is how I learned it.

Async functions are what I understand functions "turned" promises. Do I need to call .then on them each time I want to invoke an async function?

async function loadStory(){}
....
loadStory()
or
loadStory().then
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Han Che
  • 8,239
  • 19
  • 70
  • 116

1 Answers1

0

Async functions are executed as per regular functions by invoking them as usual.

In order to make your code appear synchronous and utilise the benefits of async / await you'll need to prefix the call with await.

For example:

async function example () {
    return new Promise(resolve => {
        resolve('hello');
    });
}

const myReturnedValue = await example();

Because of the await keyword, myReturnedValue will be the result of the resolved promise returned by the example function.

Alex Naish
  • 100
  • 6