blob: 386580dbd888df6afa77281bbfd2b9191a1f9bb5 (
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
|
---
title: Symbol.asyncIterator
slug: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator
tags:
- ECMAScript 2018
- JavaScript
- Property
- Reference
- Symbol
- asynchronous
translation_of: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator
---
<div>{{JSRef}}</div>
<p><strong><code>Symbol.asyncIterator</code></strong> は、オブジェクトのデフォルトの AsyncIterator を指定します。このプロパティがオブジェクトに設定されている場合、それは非同期反復可能項目であり、<code><a href="/ja/docs/Web/JavaScript/Reference/Statements/for-await...of">for await...of</a></code> ループで使用できます。</p>
<h2 id="Description" name="Description">説明</h2>
<p><code>Symbol.asyncIterator</code> シンボルは、オブジェクトの <code>@@asyncIterator</code> メソッドにアクセスするための組み込みシンボルです。オブジェクトを非同期で反復可能にするには、<code>Symbol.asyncIterator</code> キーが必要です。</p>
<p>{{js_property_attributes(0,0,0)}}</p>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="User-defined_Async_Iterables" name="User-defined_Async_Iterables">ユーザー定義の非同期反復可能項目</h3>
<p>オブジェクトに <code>[Symbol.asyncIterator]</code> プロパティを設定することで、独自の非同期イテレータを定義することができます。</p>
<pre class="brush: js notranslate">const myAsyncIterable = {
async* [Symbol.asyncIterator]() {
yield "hello";
yield "async";
yield "iteration!";
}
};
(async () => {
for await (const x of myAsyncIterable) {
console.log(x);
// 期待される出力:
// "hello"
// "async"
// "iteration!"
}
})();
</pre>
<p>API を作成するとき、非同期反復可能項目はデータのストリームやリストのような、<em>反復可能</em>なものを表すために設計されたものであり、ほとんどの状況でコールバックやイベントを完全に置き換えるものではないことに注意してください。</p>
<h3 id="Built-in_Async_Iterables" name="Built-in_Async_Iterables">組み込みの非同期反復処理</h3>
<p>現在のところ、デフォルトで <code>[Symbol.asyncIterator]</code> キーが設定されている組み込み JavaScript オブジェクトはありません。しかし、WHATWG Streams は非同期反復可能な最初の組み込みオブジェクトになるように設定されており、最近 <code>[Symbol.asyncIterator]</code> が仕様に組み込まれました。</p>
<h2 id="Specifications" name="Specifications">仕様</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-symbol.asynciterator', 'Symbol.asyncIterator')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザー実装状況</h2>
<p>{{compat("javascript.builtins.Symbol.asyncIterator")}}</p>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li><a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols">反復処理プロトコル </a></li>
<li><a href="/ja/docs/Web/JavaScript/Reference/Statements/for-await...of">for await... of</a></li>
</ul>
|