[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/async_function [Back]  [Original]

async function expression - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

async function expression

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2017 ..

async function .

async function.

In this article

async function [name]([param1[, param2[, ..., paramN]]]) {
   statements
}

ES2015, .

name

. , . .

paramN

, .

statements

, .

async function , , async function statement. async function async function , async function . async function IIFE (Immediately Invoked Function Expression), . .

js
function resolveAfter2Seconds(x) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}

(async function (x) {
  //      IIFE
  var a = resolveAfter2Seconds(20);
  var b = resolveAfter2Seconds(30);
  return x + (await a) + (await b);
})(10).then((v) => {
  console.log(v); //  60  2 .
});

var add = async function (x) {
  //     
  var a = await resolveAfter2Seconds(20);
  var b = await resolveAfter2Seconds(30);
  return x + a + b;
};

add(10).then((v) => {
  console.log(v); //  60  4 .
});

Specification
ECMAScript 2027 LanguageSpecification
# sec-async-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page