async function
키워드는 async
함수를 정의하는데 사용하는 표현식입니다.
Syntax
async function [name]([param1[, param2[, ..., paramN]]]) { statements }
Parameters
name
- 함수 이름. 함수의 이름은 오직 함수 몸체에서 지역적으로 사용되며, 함수 이름을 선언하지 않으면 익명함수로 사용된다.
paramN
- 함수에 전달되는 매개변수.
statements
- 함수를 구성하는 구문.
Description
async function
표현식은
문법과 거의 동일합니다. async function
선언async function
표현식과 async function
선언문의 주요 차이점은 익명함수로써의 사용 여부로, async function
표현식은 함수 이름을 생략하면 익명함수를 만듭니다. async function
표현식은 IIFE(즉시실행함수)로 사용할 수 있습니다. functions
문서를 참고하세요.
Examples
Simple example
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
};
var add = async function(x) { // async function 표현식을 변수에 할당
var a = await resolveAfter2Seconds(20);
var b = await resolveAfter2Seconds(30);
return x + a + b;
};
add(10).then(v => {
console.log(v); // 4초 뒤에 60 출력
});
(async function(x) { // async function 표현식을 IIFE로 사용
var p_a = resolveAfter2Seconds(20);
var p_b = resolveAfter2Seconds(30);
return x + await p_a + await p_b;
})(10).then(v => {
console.log(v); // 2초 뒤에 60 출력
});
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript Latest Draft (ECMA-262) The definition of 'async function' in that specification. |
Draft | Initial definition in ES2017. |
Browser compatibility
Update compatibility data on GitHub
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Basic support | Chrome Full support 55 | Edge Full support 15 | 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 | Edge Mobile Full support 15 | Firefox Android Full support 52 | Opera Android Full support 42 | Safari iOS Full support 10.1 | 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.
See also
async function
AsyncFunction
objectawait