aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/asyncfunction/index.html
blob: c25f3ee3a1f5747efe1ffbd66a98cbf3650e7b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
title: AsyncFunction
slug: Web/JavaScript/Reference/Global_Objects/AsyncFunction
translation_of: Web/JavaScript/Reference/Global_Objects/AsyncFunction
---
<div>{{JSRef}}</div>

<p><code><strong>Async</strong></code><strong><code>Function</code> 建構子</strong>建立一個新的 {{jsxref("Statements/async_function", "async function")}}  物件。在 JavaScript 中,每一個非同步函數實際上是一個 <code>AsyncFunction</code> 物件。</p>

<p>注意 <code>AsyncFunction</code> 不是一個全域物件。 它可以以下程式碼獲得。</p>

<pre class="brush: js">Object.getPrototypeOf(async function(){}).constructor
</pre>

<h2 id="語法">語法</h2>

<pre class="syntaxbox">new AsyncFunction([<var>arg1</var>[, <var>arg2</var>[, ...<var>argN</var>]],] <var>functionBody</var>)</pre>

<h3 id="參數">參數</h3>

<dl>
 <dt><code>arg1, arg2, ... arg<em>N</em></code></dt>
 <dd>Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "<code>x</code>", "<code>theValue</code>", or "<code>a,b</code>".</dd>
 <dt><code>functionBody</code></dt>
 <dd>一個字串。描述該函數定義的陳述式(statements)。</dd>
</dl>

<h2 id="說明">說明</h2>

<p>{{jsxref("Statements/async_function", "async function")}} objects created with the <code>AsyncFunction</code> constructor are parsed when the function is created. This is less efficient than declaring an async function with an {{jsxref("Statements/async_function", "async function expression")}} and calling it within your code, because such functions are parsed with the rest of the code.</p>

<p>All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.</p>

<div class="note">
<p><strong>Note:</strong> {{jsxref("Statements/async_function", "async functions")}} created with the <code>AsyncFunction</code> 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 <code>AsyncFunction</code> constructor was called. This is different from using {{jsxref("Global_Objects/eval", "eval")}} with code for an async function expression.</p>
</div>

<p>以函數的方式執行 <code>AsyncFunction</code> 建構式 (不使用 <code>new</code> 運算子) 和以建構子的方式執行是等效的。</p>

<h2 id="屬性">屬性</h2>

<dl>
 <dt><code><strong>AsyncFunction.length</strong></code></dt>
 <dd><code>AsyncFuction</code> 建構子的長度為 1。</dd>
 <dt>{{jsxref("AsyncFunction.prototype")}}</dt>
 <dd>可加入屬性至所有陣列物件。</dd>
</dl>

<h2 id="AsyncFunction_原型物件"><code>AsyncFunction</code> 原型物件</h2>

<h3 id="屬性_2">屬性</h3>

<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction/prototype', 'Properties')}}</div>

<h2 id="AsyncFunction_實例"><code>AsyncFunction</code> 實例</h2>

<p><code>AsyncFunction</code> 實例繼承 {{jsxref("AsyncFunction.prototype")}} 的屬性和方法. 和所有的建構子一樣,變更該建構子 (constructor) 的原型物件 (prototype object)將會影響所有的 <code>AsyncFunction</code> 實例。</p>

<h2 id="範例">範例</h2>

<h3 id="AsyncFunction_建構子建立一個非同步函數"> <code>AsyncFunction</code> 建構子建立一個非同步函數</h3>

<pre class="brush: js">function resolveAfter2Seconds(x) {
  return new Promise(resolve =&gt; {
    setTimeout(() =&gt; {
      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 =&gt; {
  console.log(v); // prints 30 after 4 seconds
});
</pre>

<h2 id="規範">規範</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-async-function-objects', 'AsyncFunction object')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>Initial definition in ES2017.</td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

<div>


<p>{{Compat("javascript.builtins.AsyncFunction")}}</p>
</div>

<h2 id="參見">參見</h2>

<ul>
 <li>{{jsxref("Statements/async_function", "async function function")}}</li>
 <li>{{jsxref("Operators/async_function", "async function expression")}}</li>
 <li>{{jsxref("Global_Objects/Function", "Function")}}</li>
 <li>{{jsxref("Statements/function", "function statement")}}</li>
 <li>{{jsxref("Operators/function", "function expression")}}</li>
 <li>{{jsxref("Functions_and_function_scope", "Functions and function scope", "", 1)}}</li>
</ul>