From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/operators/async_function/index.html | 151 +++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 files/ja/web/javascript/reference/operators/async_function/index.html (limited to 'files/ja/web/javascript/reference/operators/async_function/index.html') diff --git a/files/ja/web/javascript/reference/operators/async_function/index.html b/files/ja/web/javascript/reference/operators/async_function/index.html new file mode 100644 index 0000000000..f6b13ca9f2 --- /dev/null +++ b/files/ja/web/javascript/reference/operators/async_function/index.html @@ -0,0 +1,151 @@ +--- +title: 非同期関数式 +slug: Web/JavaScript/Reference/Operators/async_function +tags: + - Experimental + - Function + - JavaScript + - Operator + - Primary Expression +translation_of: Web/JavaScript/Reference/Operators/async_function +--- +
{{jsSidebar("Operators")}}
+ +

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

+ +

構文

+ +
async function [name]([param1[, param2[, ..., paramN]]]) {
+   statements
+}
+ +

引数

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

説明

+ +

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

+ +

+ +

シンプルな例

+ +
function resolveAfter2Seconds(x) {
+  return new Promise(resolve => {
+    setTimeout(() => {
+      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 => {
+  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 => {
+  console.log(v);  // prints 60 after 4 seconds.
+});
+
+ +

仕様

+ + + + + + + + + + + + + + + + +
仕様ステータスコメント
{{SpecName('Async Function', '#async-function-definitions', 'async function')}}{{Spec2('Async Function')}}提案
+ +

ブラウザー実装状況

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
機能ChromeFirefox (Gecko)Internet Explorer EdgeOperaSafari (WebKit)
基本サポート{{CompatChrome(55)}}{{CompatGeckoDesktop("52.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatOpera(42)}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
機能AndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
基本サポート{{CompatUnknown}}{{CompatUnknown}}{{CompatGeckoMobile("52.0")}}{{CompatUnknown}}{{CompatOpera(42)}}{{CompatUnknown}}{{CompatChrome(55)}}
+
+ +

 

+ +

関連項目

+ + -- cgit v1.2.3-54-g00ecf