JavaScript Async Functions

Introduction

Async Functions[fn:async-functions] is a new feature implemented in ES2017 to handle [JavaScript Promises](JavaScript Promises).

Syntax

Fulfilling a promise

async function asyncFunc() {
  return 123;
}
 
asyncFunc().then((x) => console.log(x));
// 123

Rejecting a promise

async function asyncFunc() {
  throw new Error("Problem!");
}
 
asyncFunc().catch((err) => console.log(err));
// Error: Problem!

Footnotes

[fn:async-functions]https://github.com/tc39/ecmascript-asyncawait