You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!
AsyncFunction 생성자는 새로운 async function 객체를 만든다. 자바스크립트에서 모든 비동기 함수는 사실상 AsyncFunction 객체이다.
AsyncFunction이 전역변수가 아님에 주의하는게 좋다. 다음의 코드를 살펴보면서 한번 알아보자.
Object.getPrototypeOf(async function(){}).constructor
문법
new AsyncFunction([arg1[, arg2[, ...argN]],] functionBody)
파라미터
arg1, arg2, ... argN- 함수에 따라 그 인수들이 정해짐에 알아야한다. 이러한 인수들은 "x","theValue",or"a,b"와 같은 자바스크립트 식별자나 콤마로 구분된 문자열과 같은게 사용되어야 한다.
functionBody- 함수 정의로 구성된 자바 스크립트 설명서를 포함하는 문자열이다.
상세설명
AsyncFunction 생성자를 통해 만들어진async function 객체는 함수가 만들어질때 분석된다 . 코드가 실행되지 않을 때도 작동하기 때문에 async function expression 으로 비동기함수를 정의하고 해당 코드에서 호출할 때보다 비효율적이다.
모든 인수들은 순서대로 생성된 함수의 파라미터의 식별자로서 함수안에서 다뤄진다.
Note: async functions created with the AsyncFunction constructor do not create closures to their creation contexts; they are always created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the AsyncFunction constructor was called. This is different from using eval with code for a async function expression.
AsyncFunction 생성자를 새로운 연산자없이 함수로서 호출하는 것은 새로운 생성자를 호출하는 것과 같다.
속성
AsyncFunction.lengthAsyncFunction생성자의 길이 속성의 값은 1이다.AsyncFunction.prototype- 모든 비동기 객체에 속성을 추가하게끔 해준다.
AsyncFunction 프로토타입 객체
속성
AsyncFunction.constructor- The initial value is
AsyncFunction. AsyncFunction.prototype[@@toStringTag]- Returns "AsyncFunction".
AsyncFunction 인스턴스
AsyncFunction AsyncFunction.prototype으로 부터의 메소드와 속성을 상속받는다. 모든 생성자와 마찬가지로, 누가나 생성자의 프로토타임 객체를 AsyncFunction 인스턴스로 만들기 위해 변경할 수 있다.
예제
AsyncFunction 생성자를 통한 비동기 함수 제작
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
var AsyncFunction = Object.getPrototypeOf(async function(){}).constructor
var a = new AsyncFunction('a',
'b',
'return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);');
a(10, 20).then(v => {
console.log(v); // prints 30 after 4 seconds
});
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript Latest Draft (ECMA-262) The definition of 'AsyncFunction object' in that specification. |
Draft | Initial definition in ES2017. |
브라우저 호환성
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-dataand send us a pull request.
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
AsyncFunction | Chrome Full support 55 | Edge Full support 15 | Firefox Full support 52 | IE No support No | Opera Full support 42 | Safari ? | WebView Android Full support 55 | Chrome Android Full support 55 | Firefox Android Full support 52 | Opera Android Full support 42 | Safari iOS ? | Samsung Internet Android Full support 6.0 | nodejs
Full support
7.6.0
|
prototype | Chrome Full support 55 | Edge Full support 15 | Firefox Full support 52 | IE No support No | Opera Full support 42 | Safari ? | WebView Android Full support 55 | Chrome Android Full support 55 | Firefox Android Full support 52 | Opera Android Full support 42 | Safari iOS ? | Samsung Internet Android Full support 6.0 | nodejs
Full support
7.6.0
|
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- User must explicitly enable this feature.
- User must explicitly enable this feature.