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

await - 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

await

Baseline Widely available *

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

* Some parts of this feature may have varying levels of support.

await Promise . .

In this article

js
await expression

expression

Promise, thenable .

thenable , thenable .

thenable , .

await Promise . await async . , await .

, await . await . , await .

expression Promise.resolve() . Promise , . expression .

  • Promise: expression Promise expression.constructor === Promise, then() await .
  • Thenable : , , , then() resolve , Promise() .
  • thenable : Promise .

, . . .

await , . await , . , await .

Promise await , Promise .

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

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

f1();

Thenable

Thenable Promise .

js
async function f2() {
  const thenable = {
    then(resolve) {
      resolve("resolved!");
    },
  };
  console.log(await thenable); // "resolved!"
}

f2();

.

js
async function f2() {
  const thenable = {
    then(_, reject) {
      reject(new Error("rejected!"));
    },
  };
  await thenable; // Throws Error: rejected!
}

f2();

Promise , await Promise . then , .

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

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

f3();

Promise , .

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

f4();

await catch() try .

js
const response = await promisedFunction().catch((err) => {
  console.error(err);
  return "default response";
});
//    "default response" .

promisedFunction() , . , .

js
function promisedFunction() {
  //        .
  return new Promise((resolve, reject) => {
    //   
  });
}

, promisedFunction() , catch() . try...catch .

await

await . await , . .

Fetch API export await . .

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

export default await colors;

await

await , await . . await , . await . , . .

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

, foo await . . , . , .

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

.

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

then() . , foo . , . await .

API , . queueMicrotask() 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() , . , await queueMicrotask() . " queueMicrotask()" .

, await .

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

  return /* await */ lastAsyncTask();
}

, lastAsyncTask .

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

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

noAwait();

// Error: failed
//    at lastAsyncTask

noAwait lastAsyncTask . , noAwait . , 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

, return await promise return promise . return promise , V8 . , return await .

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


Web Proxy Viewer  |  New URL  |  Original Page