aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/operators/async_function/index.html
blob: b2e2b5476e1e0fcbb40076e685870ca08324460d (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
---
title: 非同期関数式
slug: Web/JavaScript/Reference/Operators/async_function
tags:
  - Experimental
  - Function
  - JavaScript
  - Operator
  - Primary Expression
translation_of: Web/JavaScript/Reference/Operators/async_function
---
<div>{{jsSidebar("Operators")}}</div>

<p><strong><code>async function</code></strong> キーワードは、式内で async function を定義するために使用できます。</p>

<h2 id="構文">構文</h2>

<pre class="syntaxbox">async function [<em>name</em>]([<em>param1</em>[, <em>param2[</em>, ..., <em>paramN</em>]]]) {
   <em>statements</em>
}</pre>

<h3 id="引数">引数</h3>

<dl>
 <dt><code>name</code></dt>
 <dd>関数名。関数が<em>匿名</em>の場合、省略可能。名前は関数ボディー内のみのローカル。</dd>
 <dt><code>paramN</code></dt>
 <dd>関数に渡される引数名。</dd>
 <dt><code>statements</code></dt>
 <dd>関数ボディーを構成するステートメント。</dd>
</dl>

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

<p><code>async function</code> 式は {{jsxref('Statements/async_function', 'async function statement')}} と非常に似ており、構文もほとんど同じです。async <code>function</code> 式と async <code>function</code> ステートメントの主な違いは、<code>async function</code> 式は<em>匿名</em>関数を生成するために<em>関数名</em>を省略できる点です。<code>async function</code> 式は、定義後直ちに実行される <strong>IIFE</strong>(即時実行関数式)として使用することもできます。詳細は <a href="/ja/docs/Web/JavaScript/Reference/Functions">function</a> の章を見てください。</p>

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

<h3 id="シンプルな例">シンプルな例</h3>

<pre class="brush: js">function resolveAfter2Seconds(x) {
  return new Promise(resolve =&gt; {
    setTimeout(() =&gt; {
      resolve(x);
    }, 2000);
  });
};

(async function(x) { // async function expression used as an IIFE
  var a = resolveAfter2Seconds(20);
  var b = resolveAfter2Seconds(30);
  return x + await a + await b;
})(10).then(v =&gt; {
  console.log(v);  // prints 60 after 2 seconds.
});

var add = async function(x) { // async function expression assigned to a variable
  var a = await resolveAfter2Seconds(20);
  var b = await resolveAfter2Seconds(30);
  return x + a + b;
};

add(10).then(v =&gt; {
  console.log(v);  // prints 60 after 4 seconds.
});
</pre>

<h2 id="仕様">仕様</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様</th>
   <th scope="col">ステータス</th>
   <th scope="col">コメント</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('Async Function', '#async-function-definitions', 'async function')}}</td>
   <td>{{Spec2('Async Function')}}</td>
   <td>提案</td>
  </tr>
 </tbody>
</table>

<h2 id="ブラウザー実装状況">ブラウザー実装状況</h2>

<p>{{Compat("javascript.operators.async_function")}}</p>

<h2 id="関連項目">関連項目</h2>

<ul>
 <li>{{jsxref("Statements/async_function", "async function")}}</li>
 <li>{{jsxref("AsyncFunction")}} オブジェクト</li>
 <li>{{jsxref("Operators/await", "await")}}</li>
</ul>