如何映射异步生成器?

问题描述

假设我们有一个异步生成器:

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 proposalthis method 仍仅处于第 2 阶段。您可以使用 some polyfill,或者编写您自己的 another_method 辅助函数:

map

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...