diff options
author | Masahiro FUJIMOTO <mfujimot@gmail.com> | 2022-02-16 12:22:06 +0900 |
---|---|---|
committer | Masahiro FUJIMOTO <mfujimot@gmail.com> | 2022-02-26 16:04:26 +0900 |
commit | 4f1cf93bd612c68ce2a21a9ece6a6715b5dbc308 (patch) | |
tree | 89211be08cc534f8ee48a6fba5bd7541dfc79f85 /files/ja/web/javascript/reference/operators/async_function/index.md | |
parent | 2c5fad84705a32a9773a8d9dcf13b033ff1ca2f2 (diff) | |
download | translated-content-4f1cf93bd612c68ce2a21a9ece6a6715b5dbc308.tar.gz translated-content-4f1cf93bd612c68ce2a21a9ece6a6715b5dbc308.tar.bz2 translated-content-4f1cf93bd612c68ce2a21a9ece6a6715b5dbc308.zip |
Web/JavaScript/Reference/Operators 以下の残りの記事を移行
Diffstat (limited to 'files/ja/web/javascript/reference/operators/async_function/index.md')
-rw-r--r-- | files/ja/web/javascript/reference/operators/async_function/index.md | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/operators/async_function/index.md b/files/ja/web/javascript/reference/operators/async_function/index.md new file mode 100644 index 0000000000..f50d024a21 --- /dev/null +++ b/files/ja/web/javascript/reference/operators/async_function/index.md @@ -0,0 +1,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 => { + setTimeout(() => { + 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 => { + 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 => { + 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> |