简体   繁体   English

数组中的内置 Symbol.asyncIterator

[英]Built-in Symbol.asyncIterator in Array

According to MDN Web Docs:根据 MDN Web 文档:

There are currently no built-in JavaScript objects that have the [Symbol.asyncIterator] key set by default.当前没有默认设置 [Symbol.asyncIterator] 键的内置 JavaScript 对象。

My question is how come this is working:我的问题是这是如何工作的:

function promises() {
    return [
        new Promise(x => setTimeout(() => x(1), 1000)),
        new Promise(x => setTimeout(() => x(2), 2000)),
        new Promise(x => setTimeout(() => x(3), 3000)),
    ]
}

for await (x of promises()) console.log(x) 

MDN also states that MDN 还指出

The for await...of statement creates a loop iterating over async iterable objects as well as on sync iterables, including: built-in String, Array, Array-like objects (eg, arguments or NodeList), TypedArray, Map, Set, and user-defined async/sync iterables - MDN (emphasis mine) for await...of语句创建一个循环迭代异步可迭代对象以及同步可迭代对象,包括:内置字符串、数组、类数组对象(例如 arguments 或 NodeList)、TypedArray、Map、Set、和用户定义的异步/同步迭代- MDN (强调我的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 For..of 和 Symbol.asyncIterator - For..of with Symbol.asyncIterator 使用javascript的Symbol.asyncIterator与等待循环 - Using javascript's Symbol.asyncIterator with for await of loop 制作一个没有 async/await 但只有 Promise 的异步生成器 *[Symbol.asyncIterator] - Making an asynchronous generator *[Symbol.asyncIterator] without async/await but with promises only 创建异步迭代器的最佳实践是什么? 我应该使用异步生成器 function 还是使用 Symbol.asyncIterator? - What is the best practice to create an async iterator? Should I use an async generator function or rather use Symbol.asyncIterator? 如何检查数组是否是普通的内置数组? - How to check if an array is a plain built-in Array? 重用内置数组方法 - JS - 困惑 - Reusing built-in array methods - JS - Confused 在JavaScript中将数组作为内置函数参数传递 - Passing an array as a built-in function parameter in JavaScript 有关不同内置数组方法的术语Javascript - Terminology regarding different built-in array methods Javascript 向Array添加自定义方法时的最佳实践(内置对象) - Best practice when adding custom method to Array (Built-in object) 使用 ES6 类扩展内置数组 - 方法不是 function 错误 - Extending Built-in Array With ES6 Classes - Method is not a function error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM