[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function [Back]  [Original]

async function - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

async function

20174

async function await promise promise

async function

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

async function asyncCall() {
  console.log("calling");
  const result = await resolveAfter2Seconds();
  console.log(result);
  // Expected output: "resolved"
}

asyncCall();

js
async function name(param0) {
  statements
}
async function name(param0, param1) {
  statements
}
async function name(param0, param1, /* , */ paramN) {
  statements
}

async function JavaScript async function

name

param

statements

await

async function AsyncFunction Promise

await await promise promise promise await async/await try/catch

await JavaScript SyntaxError

await JavaScript

async/await promise API async/await promise

promise promise promise

js
async function foo() {
  return 1;
}

js
function foo() {
  return Promise.resolve(1);
}

Promise.resolve

promise Promise.resolve

promise

js
const p = new Promise((res, rej) => {
  res(1);
});

async function asyncReturn() {
  return p;
}

function basicReturn() {
  return Promise.resolve(p);
}

console.log(p === basicReturn()); // true
console.log(p === asyncReturn()); // false

await await await await

js
async function foo() {
  await 1;
}

js
function foo() {
  return Promise.resolve(1).then(() => undefined);
}

await .then promise

await promise foo

  1. foo await promise foo foo
  2. promise foo promise await 1 result1 await foo
  3. promise foo promise await 2 result2 undefined promise
js
async function foo() {
  const result1 = await new Promise((resolve) =>
    setTimeout(() => resolve("1")),
  );
  const result2 = await new Promise((resolve) =>
    setTimeout(() => resolve("2")),
  );
}
foo();

promise promise

promise .catch promise p2 p1 promise

js
async function foo() {
  const p1 = new Promise((resolve) => setTimeout(() => resolve("1"), 1000));
  const p2 = new Promise((_, reject) => setTimeout(() => reject("2"), 500));
  const results = [await p1, await p2]; //  Promise.all  Promise.allSettled
}
foo().catch(() => {}); // ...

async function function

js
function resolveAfter2Seconds() {
  console.log(" promise");
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("slow");
      console.log(" promise ");
    }, 2000);
  });
}

function resolveAfter1Second() {
  console.log(" promise");
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("fast");
      console.log(" promise ");
    }, 1000);
  });
}

async function sequentialStart() {
  console.log("== sequentialStart  ==");

  // 1. 
  const slow = resolveAfter2Seconds();
  console.log(await slow);

  // 2. 
  const fast = resolveAfter1Second();
  console.log(await fast);

  console.log("== sequentialStart  ==");
}

async function sequentialWait() {
  console.log("== sequentialWait  ==");

  // 1. 
  const slow = resolveAfter2Seconds();
  const fast = resolveAfter1Second();

  // 2. 
  console.log(await slow);
  // 3. 
  console.log(await fast);

  console.log("== sequentialWait  ==");
}

async function concurrent1() {
  console.log("== concurrent1  ==");

  // 1. 
  const results = await Promise.all([
    resolveAfter2Seconds(),
    resolveAfter1Second(),
  ]);
  // 2. 
  console.log(results[0]);
  console.log(results[1]);

  console.log("== concurrent1  ==");
}

async function concurrent2() {
  console.log("== concurrent2  ==");

  // 1. 
  await Promise.all([
    (async () => console.log(await resolveAfter2Seconds()))(),
    (async () => console.log(await resolveAfter1Second()))(),
  ]);
  console.log("== concurrent2  ==");
}

sequentialStart(); // 2 slow 1 fast

// 
setTimeout(sequentialWait, 4000); // 2 slowfast

// 
setTimeout(concurrent1, 7000); //  sequentialWait 

// 
setTimeout(concurrent2, 10000); // 1 fast 1 slow

await

sequentialStart await 2 await 1 3

sequentialWait await 2 3 await await

concurrentStart await 2 3

Promise.all() Promise.allSettled()

sequentialWait concurrent1

sequentialWait promise promise promise catch

concurrent1 Promise.all promise promise promise

promise

Promise API promise

js
function getProcessedData(url) {
  return downloadData(url) //  promise
    .catch((e) => downloadFallbackData(url)) //  promise
    .then((v) => processDataInWorker(v)); //  promise
}

js
async function getProcessedData(url) {
  let v;
  try {
    v = await downloadData(url);
  } catch (e) {
    v = await downloadFallbackData(url);
  }
  return processDataInWorker(v);
}

catch() promise

js
async function getProcessedData(url) {
  const v = await downloadData(url).catch((e) => downloadFallbackData(url));
  return processDataInWorker(v);
}

return await Promise.resolve promise

ECMAScript 2027 LanguageSpecification
# sec-async-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page