I am trying to create async generator function in Node.js, but it seems to be impossible.
Version of my Node.js: 7.6.0.
My code:
async function* async_generator(){
for(let i = 0; i < 10; i++){
yield await call_to_async_func(i);
};
}
Error I got:
Does anyone knows what is the problem? Why I can't create async generator function while I can create generator function or async function Independently?
I am trying to create async generator function in Node.js, but it seems to be impossible.
Version of my Node.js: 7.6.0.
My code:
async function* async_generator(){
for(let i = 0; i < 10; i++){
yield await call_to_async_func(i);
};
}
Error I got:
Does anyone knows what is the problem? Why I can't create async generator function while I can create generator function or async function Independently?
Share Improve this question edited Jul 3, 2017 at 11:21 Bugs 4,4899 gold badges33 silver badges41 bronze badges asked Jul 3, 2017 at 10:45 EmilEmil 6811 gold badge7 silver badges22 bronze badges 2- Async generators don’t exist as of ES2018 as far as I know? – Ry- ♦ Commented Jul 3, 2017 at 10:47
- There was a proposal (here) but it's been withdrawn. – robertklep Commented Jul 3, 2017 at 10:49
2 Answers
Reset to default 8It is there and it does work, but currently it is behind a harmony flag.
example.js
async function* async_generator() {
for (let i = 0; i < 10; i++) {
yield await new Promise(r => setTimeout(_ => r("hello world"), 100))
};
}
async function main(){
for await (let item of async_generator()){
console.log(item);
}
}
main().catch(console.log);
run with (works for me in node v8.5.0)
node --harmony-async-iteration example.js
be aware that the proposal is still at stage-3 and if you want to use it in the browser you'll likely also need to transpile with typescript or babel.
update:
as of node 9, async generators are staged. You can enable it simply with --harmony
.
There simply are no asynchronous generator functions in Node.js.
Yet. They're still figuring out what their semantics would be, see the async iteration proposal.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742313707a4420409.html
评论列表(0条)