From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/symbol/asynciterator/index.html | 76 ++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator') diff --git a/files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator/index.html b/files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator/index.html new file mode 100644 index 0000000000..f0c05da9ab --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator/index.html @@ -0,0 +1,76 @@ +--- +title: Symbol.asyncIterator +slug: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator +translation_of: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator +--- +
{{JSRef}}
+ +

Symbol.asyncIterator 符号指定了一个对象的默认异步迭代器。如果一个对象设置了这个属性,它就是异步可迭代对象,可用于for await...of循环。

+ + + +

描述

+ +

Symbol.asyncIterator 是一个用于访问对象的@@asyncIterator方法的内建符号。一个异步可迭代对象必须要有Symbol.asyncIterator属性。

+ +

{{js_property_attributes(0,0,0)}}

+ +

示例

+ +

自定义异步可迭代对象

+ +

你可以通过设置[Symbol.asyncIterator]属性来自定义异步可迭代对象。

+ +
const myAsyncIterable = new Object();
+myAsyncIterable[Symbol.asyncIterator] = async function*() {
+    yield "hello";
+    yield "async";
+    yield "iteration!";
+};
+
+(async () => {
+    for await (const x of myAsyncIterable) {
+        console.log(x);
+        // expected output:
+        //    "hello"
+        //    "async"
+        //    "iteration!"
+    }
+})();
+
+ +

内建异步可迭代对象

+ +

目前没有默认设定了[Symbol.asyncIterator]属性的JavaScript内建的对象。不过,WHATWG(网页超文本应用技术工作小组)Streams会被设定为第一批异步可迭代对象,[Symbol.asyncIterator] 最近已在设计规范中落地。

+ +

规范

+ + + + + + + + + + + + + + + + +
标准状态注释
{{SpecName('ES2018', '#sec-symbol.asynciterator', 'Symbol.asyncIterator')}}{{Spec2('ES2018')}}
+ +

浏览器兼容性

+ + + +

{{compat("javascript.builtins.Symbol.asyncIterator")}}

+ +

参见

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