hx-jsasync icon indicating copy to clipboard operation
hx-jsasync copied to clipboard

how to write for await ...of or AsyncGenerator in hx-jsasync?

Open sonygod opened this issue 3 years ago • 3 comments

how to write haxe code eq

  for await (const msg of source) {
 // exact matching
  node2.handle('/your-protocol', ({ stream }) => {
    pipe(
      stream,
      async function (source) {
        for await (const msg of source) {
          console.log(msg.toString())
        }
      }
    )
  })

how to support AsyncGenerator?

		node.handle('/your-protocol', (d) -> {
			ItPipe.pipe(d.stream, JSAsync.jsasync(function(source:Array<Promise<Any>>) {
				
				

				//don't know how to write here?
				
			}));

			
		});

sonygod avatar Aug 19 '21 08:08 sonygod

hx-jsasync doesn't currently support for await. It might be a good feature to include in the future.

In your example code the source argument of your function is an Array and not an async iterator/generator. If so you can simply do:

for (v in source) {
  Browser.console.log(msg.jsawait());
}

However, if your source is an async iterator you'll need to write the loop manually:

while(true) {
  var v  = source.next().jsawait();
  if ( v.done ) break;
  Browser.console.log(v.value)
}

basro avatar Aug 19 '21 11:08 basro

thank you @basro , shall we keep open this until you add a new feature about for await of

sonygod avatar Aug 20 '21 02:08 sonygod

Sure

basro avatar Aug 20 '21 03:08 basro