| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function | [Back] [Original] |
Get to know MDN better
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();
async function name(param0) {
statements
}
async function name(param0, param1) {
statements
}
async function name(param0, param1, /* , */ paramN) {
statements
}
nameparam statements await
async function AsyncFunction Promise
await await promise promise promise await async/await try/catch
await JavaScript SyntaxError
await JavaScript
promise promise promise
async function foo() {
return 1;
}
function foo() {
return Promise.resolve(1);
}
Promise.resolve
promise Promise.resolve
promise
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
async function foo() {
await 1;
}
function foo() {
return Promise.resolve(1).then(() => undefined);
}
await .then promise
await promise foo
foo await promise foo foo foo promise await 1 result1 await foo foo promise await 2 result2 undefined promise 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
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
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
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 API promise
function getProcessedData(url) {
return downloadData(url) // promise
.catch((e) => downloadFallbackData(url)) // promise
.then((v) => processDataInWorker(v)); // promise
}
async function getProcessedData(url) {
let v;
try {
v = await downloadData(url);
} catch (e) {
v = await downloadFallbackData(url);
}
return processDataInWorker(v);
}
catch() promise
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 |
AsyncFunctionasync function functionfunction*async function*awaitPromise| Web Proxy Viewer | New URL | Original Page |