How to declare an asynchronous generator function

I am trying to create an async generator function in Node.js, but this seems impossible.

The version of my Node.js is 7.6.0.

My code is :

async function* async_generator(){
  for(let i = 0; i < 10; i++){
    yield await call_to_async_func(i);
  };
}

Error. I received :

enter image description here

Does anyone know what the problem is? Why can't I create an async generator function while I can create a generator function or async function myself?

+4
source share
2 answers

There are simply no asynchronous generator functions.

Nonetheless. They are still figuring out what their semantics will be, see asynchronous sentence .

+1
source

, , .

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);

( node v8.5.0)

node --harmony-async-iteration example.js

, 3, , , , typescript babel.

update:

node 9, . --harmony.

+2

Source: https://habr.com/ru/post/1680628/


All Articles