| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/await | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
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 . .
await expression
thenable , thenable .
thenable , .
await Promise . await async . , await .
expression Promise.resolve() . Promise , . expression .
Promise: expression Promise expression.constructor === Promise, then() await .then() resolve , Promise() .Promise ., . . .
await , . await , . , await .
Promise await , Promise .
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 Promise .
async function f2() {
const thenable = {
then(resolve) {
resolve("resolved!");
},
};
console.log(await thenable); // "resolved!"
}
f2();
.
async function f2() {
const thenable = {
then(_, reject) {
reject(new Error("rejected!"));
},
};
await thenable; // Throws Error: rejected!
}
f2();
Promise , await Promise . then , .
async function f3() {
const y = await 20;
console.log(y); // 20
const obj = {};
console.log((await obj) === obj); // true
}
f3();
Promise , .
async function f4() {
try {
const z = await Promise.reject(30);
} catch (e) {
console.error(e); // 30
}
}
f4();
await catch() try .
const response = await promisedFunction().catch((err) => {
console.error(err);
return "default response";
});
// "default response" .
promisedFunction() , . , .
function promisedFunction() {
// .
return new Promise((resolve, reject) => {
//
});
}
, promisedFunction() , catch() . try...catch .
// fetch
const colors = fetch("../data/colors.json").then((response) => response.json());
export default await colors;
await , await . . await , . await . , . .
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 . . , . , .
function foo(name) {
return new Promise((resolve) => {
console.log(name, "start");
console.log(name, "middle");
console.log(name, "end");
resolve();
});
}
await , .
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
.
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 .
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 .
async function noAwait() {
// ...
return /* await */ lastAsyncTask();
}
, lastAsyncTask .
async function lastAsyncTask() {
await null;
throw new Error("failed");
}
async function noAwait() {
return lastAsyncTask();
}
noAwait();
// Error: failed
// at lastAsyncTask
noAwait lastAsyncTask . , noAwait . , await . , .
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 |
This page was last modified on 2025 8 8 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |