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

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

<p>非同期関数は、 <a
    href="/ja/docs/Web/JavaScript/Reference/Statements/async_function">async function 文</a>を使用して定義することもできます。</p>

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

<pre class="brush: js">async function [<var>name</var>]([<var>param1</var>[, <var>param2</var>[, ..., <var>paramN</var>]]]) {
   <var>statements</var>
}</pre>

<p>ES2015 では、<a href="/ja/docs/Web/JavaScript/Reference/Functions/Arrow_functions">アロー関数</a>を使用することもできます。</p>

<h3 id="Parameters">引数</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="Description">解説</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">関数</a>の章を見てください。</p>

<h2 id="Examples"></h2>

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

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

const add = async function(x) { // 変数に代入された非同期関数式
  let a = await resolveAfter2Seconds(20);
  let b = await resolveAfter2Seconds(30);
  return x + a + b;
};

add(10).then(v =&gt; {
  console.log(v);  // 4 秒後に 60 を表示
});

(async function(x) { // IIFE として使用される非同期関数式
  let p_a = resolveAfter2Seconds(20);
  let p_b = resolveAfter2Seconds(30);
  return x + await p_a + await p_b;
})(10).then(v =&gt; {
  console.log(v);  // 2 秒後に 60 を表示
});
</pre>

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

<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">仕様書</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{SpecName('ESDraft', '#sec-async-function-definitions', 'async function')}}</td>
    </tr>
  </tbody>
</table>

<h2 id="Browser_compatibility">ブラウザーの互換性</h2>

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

<h2 id="See_also">関連情報</h2>

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