Baseline Widely available
async function
ì ì¸ì AsyncFunction
ê°ì²´ë¥¼ ë°ííë íëì ë¹ë기 í¨ì를 ì ìí©ëë¤. ë¹ë기 í¨ìë ì´ë²¤í¸ 루í를 íµí´ ë¹ë기ì ì¼ë¡ ìëíë í¨ìë¡, ììì ì¼ë¡ Promise
를 ì¬ì©íì¬ ê²°ê³¼ë¥¼ ë°íí©ëë¤. ê·¸ë¬ë ë¹ë기 í¨ì를 ì¬ì©íë ì½ëì 구문과 구조ë, íì¤ ë기 í¨ì를 ì¬ì©íëê²ê³¼ ë§ì´ ë¹ì·í©ëë¤.
ëí async function expressionì ì¬ì©í´ì 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();
Syntax
async function name([param[, param[, ... param]]]) {
statements
}
매ê°ë³ì
name
í¨ìì ì´ë¦.
param
Optional
í¨ììê² ì ë¬ë기 ìí ì¸ìì ì´ë¦.
statements
Optional
í¨ì 본문ì 구ì±íë ë´ì©. await ë©ì»¤ëì¦ì´ ì¬ì©ë ì ìë¤.
async
í¨ììë await
ìì´ í¬í¨ë ì ììµëë¤. ì´ ìì async
í¨ìì ì¤íì ì¼ì ì¤ì§íê³ ì ë¬ ë Promise
ì í´ê²°ì 기ë¤ë¦° ë¤ì async
í¨ìì ì¤íì ë¤ì ììíê³ ìë£í ê°ì ë°íí©ëë¤.
Note:
await
í¤ìëëasync
í¨ìììë§ ì í¨íë¤ë ê²ì 기ìµíììì¤.async
í¨ìì 본문 ì¸ë¶ìì ì¬ì©íë©´ SyntaxErrorê° ë°ìí©ëë¤.
ì°¸ê³ : async/awaití¨ìì 목ì ì ì¬ì©íë ì¬ë¬ promiseì ëìì ë기ì¤ë½ê² ì¬ì©í ì ìê² íê³ , ì´ë í ëìì ì¬ë¬ promiseì 그룹ìì ê°ë¨íê² ëìíê² íë ê²ì´ë¤. promiseê° êµ¬ì¡°íë callbackê³¼ ì ì¬í ê² ì²ë¼ async/await
ëí ì ë¤ë ì´í°(generator)ì íë¡ë¯¸ì¤(promise)를 묶ëê²ê³¼ ì ì¬íë¤.
async
í¨ìë íì promise를 ë°íí©ëë¤. ë§ì½ async
í¨ìì ë°íê°ì´ ëª
ìì ì¼ë¡ promiseê° ìëë¼ë©´ ì묵ì ì¼ë¡ promiseë¡ ê°ì¸ì§ëë¤.
ì를 ë¤ì´
async function foo() {
return 1;
}
ì ì½ëë ìëì ê°ìµëë¤.
function foo() {
return Promise.resolve(1);
}
async
í¨ìì 본문ì 0ê° ì´ìì await
문ì¼ë¡ ë¶í ë ê²ì¼ë¡ ìê°í ì ììµëë¤. 첫ë²ì§¸ await
문ì í¬í¨íë ìµìì ì½ëë ë기ì ì¼ë¡ ì¤íë©ëë¤. ë°ë¼ì await
ë¬¸ì´ ìë async
í¨ìë ë기ì ì¼ë¡ ì¤íë©ëë¤. íì§ë§ await
ë¬¸ì´ ìë¤ë©´ async
í¨ìë íì ë¹ë기ì ì¼ë¡ ìë£ë©ëë¤.
ì를 ë¤ì´
async function foo() {
await 1;
}
ì ì½ëë ìëì ê°ìµëë¤.
function foo() {
return Promise.resolve(1).then(() => undefined);
}
Examples Simple example
var resolveAfter2Seconds = function () {
console.log("starting slow promise");
return new Promise((resolve) => {
setTimeout(function () {
resolve(20);
console.log("slow promise is done");
}, 2000);
});
};
var resolveAfter1Second = function () {
console.log("starting fast promise");
return new Promise((resolve) => {
setTimeout(function () {
resolve(10);
console.log("fast promise is done");
}, 1000);
});
};
var sequentialStart = async function () {
console.log("==SEQUENTIAL START==");
// If the value of the expression following the await operator is not a Promise, it's converted to a resolved Promise.
const slow = await resolveAfter2Seconds();
console.log(slow);
const fast = await resolveAfter1Second();
console.log(fast);
};
var concurrentStart = async function () {
console.log("==CONCURRENT START with await==");
const slow = resolveAfter2Seconds(); // starts timer immediately
const fast = resolveAfter1Second();
console.log(await slow);
console.log(await fast); // waits for slow to finish, even though fast is already done!
};
var stillConcurrent = function () {
console.log("==CONCURRENT START with Promise.all==");
Promise.all([resolveAfter2Seconds(), resolveAfter1Second()]).then(
(messages) => {
console.log(messages[0]); // slow
console.log(messages[1]); // fast
},
);
};
var parallel = function () {
console.log("==PARALLEL with Promise.then==");
resolveAfter2Seconds().then((message) => console.log(message));
resolveAfter1Second().then((message) => console.log(message));
};
sequentialStart(); // after 2 seconds, logs "slow", then after 1 more second, "fast"
// wait above to finish
setTimeout(concurrentStart, 4000); // after 2 seconds, logs "slow" and then "fast"
// wait again
setTimeout(stillConcurrent, 7000); // same as concurrentStart
// wait again
setTimeout(parallel, 10000); // trully parallel: after 1 second, logs "fast", then after 1 more second, "slow"
Warning:
await
ìPromise#then
ì í¼ëíì§ ë§ì¸ì.sequentialStart
ìì, 첫 ë²ì§¸await
ë 2ì´ì ë기 ìê°ì ê°ê³ , ë¤ì ë ë²ì§¸await
ìì 1ì´ì ë기 ìê°ì ê°ìµëë¤. ë ë²ì§¸ íì´ë¨¸ë 첫 ë²ì§¸ íì´ë¨¸ê° ìë£ë ë ê¹ì§ ìì±ëì§ ììµëë¤.concurrentStart
ìì, ë íì´ë¨¸ 모ë ìì± ë ë¤ìawait
í©ëë¤. íì´ë¨¸ê° ëìì ì¤íëê³ ìì§ë§,await
í¸ì¶ì ì¬ì í ì°ìì ì¤íì¤ì´ë¯ë¡, ë ë²ì§¸await
ë 첫 ë²ì§¸ í¸ì¶ì´ ëë ë ê¹ì§ ë기í©ëë¤. ì´ë ê²íë©´ 3ì´ê° ìëë¼, ê°ì¥ ë린 íì´ë¨¸ì íìí 2ì´ê° íìí©ëë¤.stillConcurrent
ììëPromise.all
ì ì¬ì©íì¬ ê°ì ì¼ì´ ë°ìí©ëë¤. ë ê° ì´ìì íë¬ë¯¸ì¤ë¥¼ ëìì wait íê³ ì¶ë¤ë©´,Promise#then
ì ì¬ì©íì¬ ìì ì ê°ì´parallel
를 ìíí ì ììµëë¤.
async
í¨ì를 ì¬ì©í promise chain ì¬ìì±
Promise
를 ë°ííë APIë promise chainì ë§ë¤ë©° ì¬ë¬ íí¸ì í¨ìë¡ ëëë¤. ìë ì½ë를 ë³´ì.
function getProcessedData(url) {
return downloadData(url) // returns a promise
.catch((e) => {
return downloadFallbackData(url); // returns a promise
})
.then((v) => {
return processDataInWorker(v); // returns a promise
});
}
ìì ì½ëë íëì asyncí¨ìë¡ ìëì ê°ì´ ì°ì¬ì§ ìë ìë¤.
async function getProcessedData(url) {
let v;
try {
v = await downloadData(url);
} catch (e) {
v = await downloadFallbackData(url);
}
return processDataInWorker(v);
}
ì ìì ììë return 구문ì await êµ¬ë¬¸ì´ ìë¤ë ê²ì 주목íì. ì´ë async functionì ë°íê°ì´ ì묵ì ì¼ë¡ Promise.resolve
ë¡ ê°ì¸ì§ê¸° ë문ì´ë¤.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4