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

await - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

await

*

20173

*

await Promise

js
await expression;

expression

Promise Thenable

Promise thenable thenable

reject

await promise Promise expression await Promise settledresumeawait Promise

Promise rejectedawait reasonawait stack trace

await Promise.resolve() Promise Promise

  • PromisePromise expression.constructor === Promise then()
  • thenable Promise polyfillProxy Promise then()
  • thenable Promise

Promise

Promise await await Promise Promise

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

async function f1() {
  let x = await resolveAfter2Seconds(10);
  console.log(x); // 10
}

f1();

promise

Promiseawait Promise

js
async function f3() {
  const y = await 20;
  console.log(y); // 20

  const obj = {};
  console.log((await obj) === obj); // true
}

f3();

promise

Promise

js
async function f4() {
  try {
    const z = await Promise.reject(30);
  } catch (e) {
    console.error(e); // 30
  }
}

f4();

promise

catch() try promise promise

js
const response = await promisedFunction().catch((err) => {
  console.error(err);
  return "default response";
});
// response will be "default response" if the promise is rejected

await

await await

export Fetch API fetch

js
// fetch request
const colors = fetch("../data/colors.json").then((response) => response.json());

export default await colors;

await

await microtask queue promise promise

js
async function foo(name) {
  console.log(name, "start");
  console.log(name, "middle");
  console.log(name, "end");
}

foo("First");
foo("Second");

// First start
// First middle
// First end
// Second start
// Second middle
// Second end

Promise

js
function foo(name) {
  return new Promise((resolve) => {
    console.log(name, "start");
    console.log(name, "middle");
    console.log(name, "end");
    resolve();
  });
}

await

js
async function foo(name) {
  console.log(name, "start");
  await console.log(name, "middle");
  console.log(name, "end");
}

foo("First");
foo("Second");

// First start
// First middle
// Second start
// Second middle
// First end
// Second end

Promise

js
function foo(name) {
  return new Promise((resolve) => {
    console.log(name, "start");
    resolve(console.log(name, "middle"));
  }).then(() => {
    console.log(name, "end");
  });
}

then() Promise then() await Promise await

js
let i = 0;

queueMicrotask(function test() {
  i++;
  console.log("microtask", i);
  if (i < 3) {
    queueMicrotask(test);
  }
});

(async () => {
  console.log("async function start");
  for (let i = 1; i < 3; i++) {
    await null;
    console.log("async function resume", i);
  }
  await null;
  console.log("async function end");
})();

queueMicrotask(() => {
  console.log("queueMicrotask() after calling async function");
});

console.log("script sync part end");

// Logs:
// async function start
// script sync part end
// microtask 1
// async function resume 1
// queueMicrotask() after calling async function
// microtask 2
// async function resume 2
// microtask 3
// async function end

test() console.log("queueMicrotask() after calling async function"); await "queueMicrotask() after calling async function"

Promise await

js
async function noAwait() {
  // Some actions...

  return /* await */ lastAsyncTask();
}

Promise

js
async function lastAsyncTask() {
  await null;
  throw new Error("failed");
}

async function noAwait() {
  return lastAsyncTask();
}

noAwait();

// Error: failed
//    at lastAsyncTask

lastAsyncTask noAwait Promise noAwait await Promise await

js
async function lastAsyncTask() {
  await null;
  throw new Error("failed");
}

async function withAwait() {
  return await lastAsyncTask();
}

withAwait();

// Error: failed
//    at lastAsyncTask
//    at async withAwait

Promise

ECMAScript 2027 LanguageSpecification
# sec-async-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page