concept async function in category javascript

appears as: sync functions, n async function, An async function, async function, sync function
Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

This is an excerpt from Manning's book Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond.

The awesome thing about learning how to write asynchronous code using generators, promises, and a runner function is that you don’t have to learn anything new to make the jump to asynchronous functions. That’s because async functions are just syntactic sugar for the generators + promises approach. All you really need to learn are the new keywords. To indicate that a function is an async function, you prefix it with the async keyword. Then, instead of using the keyword yield, you use the keyword await. That’s all!

The previous fetch image function could be rewritten using an async function:

async function fail() {
  const msg = await Promise.reject('I failed.');
  return 'I Succeeded.';
};

fail().then(msg => console.log(msg), msg => console.log(msg))        1
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":5,\"ch\":69},{\"line\":5,\"ch\":70}]]"}
!@%STYLE%@!

In this lesson, you learned how async functions work and how to use them.

  • An async function is syntactic sugar for a generator and a runner function.