如何在 JavaScript 中使用异步生成器?

Che*_*het 5 javascript generator node.js async-await

我有一个 api,它将返回一个用于获取更多数据的游标。我是这样模拟的:

function fetch(n) {
  return Promise.resolve({
    results: [n],
    next: next < 10 && n + 1,
  })
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是弄清楚如何将 async/await 与生成器一起使用,以便与这个 api 进行交互。

这基本上是我的原型:

async function* api(url) {
  let result = await fetch(url)
  yield result
  while (result.next) {
    result = await fetch(result.next)
    yield result
  }
}
Run Code Online (Sandbox Code Playgroud)

这个想法是我应该能够创建一个异步生成器并从该生成器中产生,以便遍历游标:

async function main() {
  const gen = api(0)
  const zero = await gen.next()
  console.log(zero.result)
  const one = await gen.next()
  console.log(one.result)
  const rest = await Promise.all([...gen])
  console.log(rest.map(r => r.result))
}
Run Code Online (Sandbox Code Playgroud)

考虑到所有因素,我认为这是处理分页数据的一种非常好的方式,并且能够提取所有数据[...gen]非常酷。

唯一的问题是,它不起作用!显然你不能使用asyncwith function*

??? node --version
v7.0.0
??? node --harmony --harmony-async-await async-generator.js
/Users/chetcorcos/code/async-generator.js:11
async function* api(url) {
              ^
SyntaxError: Unexpected token *
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:545:28)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:382:7)
    at startup (bootstrap_node.js:137:9)
    at bootstrap_node.js:497:3
Run Code Online (Sandbox Code Playgroud)

但我真的觉得这应该是可能的。有一个名为co的流行库,我一直在探索它,但我认为这不是我想要的。

任何想法如何让“异步生成器”这个概念起作用?

gue*_*314 0

您可以传递调用生成器函数作为参数,而不使用扩展元素,Promise.all()接受可迭代作为yield返回的参数。请注意,Promise.all()不会按Promise顺序解析或拒绝传递的对象,但会以与传递的可迭代对象中的元素相同的顺序返回结果数组。

let api = (value) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(value)
    }, Math.floor(Math.random() * 3500))
  })
};

let values = [1, 2, 3];
let results = [];
let gen = function* gen(fn, props) {
  let i = 0; 
  do {
    yield fn(props[i]).then(res => {console.log(res); return res});
    ++i;
  } while (i < props.length);
}

Promise.all(gen(api, values))
.then(data => console.log("complete:", data))
.catch(err => console.log(err));
Run Code Online (Sandbox Code Playgroud)