问题描述
假设我们有一个异步生成器:
exports.asyncGen = async function* (items) {
for (const item of items) {
const result = await someAsyncFunc(item)
yield result;
}
}
可以映射到这个生成器吗?基本上我想这样做:
const { asyncGen } = require('./asyncGen.js')
exports.process = async function (items) {
return asyncGen(items).map(item => {
//... do something
})
}
截至目前,.map
无法识别异步迭代器。
另一种方法是使用 for await ... of
,但这远不及 .map
解决方法
提供 iterator methods proposal 的 this method 仍仅处于第 2 阶段。您可以使用 some polyfill,或者编写您自己的 another_method
辅助函数:
map