번역이 완료되지 않았습니다. Please help translate this article from English
구문
[rv] = await expression;
설명
await 문은 async함수의 실행을 중단시키고, Promise가 fulfill되거나 reject되기를 기다리고, 다시 async함수를 실행시킵니다. 이때 await 문의 값은 Promise 에서 fulfill된 값이 됩니다.
만약 Promise가 reject되면, await은 reject된 값을 throw합니다.
await 연산자 다음에 나오는 문의 값이 Promise가 아니면 해당 값을 resolved Promise로 변환시킵니다.
An await can split execution flow, allowing the caller of the await's function to resume execution before the deferred continuation of the await's function. After the await defers the continuation of its function, if this is the first await executed by the function, immediate execution also continues by returning to the function's caller a pending Promise for the completion of the await's function and resuming execution of that caller.
예제
만약 Promise가 await에 넘겨지면, await은 Promise가 fulfill되기를 기다렸다가, 해당 값을 리턴합니다.
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function f1() {
var x = await resolveAfter2Seconds(10);
console.log(x); // 10
}
f1();
Thenable objects will be fulfilled just the same.
async function f2() {
const thenable = {
then: function(resolve, _reject) {
resolve('resolved!')
}
};
console.log(await thenable); // resolved!
}
f2();
만약 값이 Promise가 아니라면, 해당 값은 resolve된 Promise로 변환되며 이를 기다립니다.
async function f2() {
var y = await 20;
console.log(y); // 20
}
f2();
만약 Promise가 reject되면, reject된 값이 throw됩니다.
async function f3() {
try {
var z = await Promise.reject(30);
} catch(e) {
console.log(e); // 30
}
}
f3();
try블럭 없이 rejected Promise다루기
var response = await promisedFunction().catch((err) => { console.error(err); });
// response will be undefined if the promise is rejected
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript (ECMA-262) The definition of 'async functions' in that specification. |
Living Standard | |
| ECMAScript 2018 (ECMA-262) The definition of 'async functions' in that specification. |
Standard | |
| ECMAScript 2017 (ECMA-262) The definition of 'async functions' in that specification. |
Standard | Initial definition. |
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
await | Chrome Full support 55 | Edge Full support 14 | Firefox Full support 52 | IE No support No | Opera Full support 42 | Safari Full support 10.1 | WebView Android Full support 55 | Chrome Android Full support 55 | Firefox Android Full support 52 | Opera Android Full support 42 | Safari iOS Full support 10.3 | Samsung Internet Android Full support 6.0 | nodejs
Full support
7.6.0
|
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.