我有一个 api 将返回一个游标以获取更多数据。我像这样 mock 它:
function fetch(n) {
return Promise.resolve({
results: [n],
next: next < 10 && n + 1,
})
}
我想做的是弄清楚如何将 async/await 与生成器一起使用,以便与此 api 进行交互。
这基本上是我制作的原型(prototype):
async function* api(url) {
let result = await fetch(url)
yield result
while (result.next) {
result = await fetch(result.next)
yield result
}
}
我的想法是我应该能够创建一个异步生成器并从该生成器中产生以遍历游标:
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))
}
考虑到所有因素,我认为这是处理分页数据的一种非常好的方式,并且能够使用 [...gen]
提取所有数据真是太酷了。
唯一的问题是,它不起作用!显然你不能将 async
与 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
但我真的觉得这应该是可能的。有一个流行的图书馆叫做 co我一直在研究,但我认为这不是我想要的。
有什么想法可以让“异步生成器”的概念发挥作用吗?
最佳答案
您可以使用 Babel 插件来做到这一点 transform-async-generator-functions .
用法是这样的:
const g = async i => [ 1, 2, 3 ]
.map(x => x * 10 ** i);
const f = async function * () {
for (let i = 0; i < 10; i++) {
const xs = await g(i);
for (const x of xs) {
yield x;
}
}
};
const main = async () => {
for await (const x of f()) {
console.log(x);
}
};
main().catch(e => console.error(e));
这里是 an example repo显示如何设置您的项目。
重要的部分是.babelrc
文件:
{
"presets": [ "env" ],
"plugins": [ "transform-async-generator-functions" ]
}
关于javascript - 如何在 JavaScript 中使用异步生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41350787/