Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning an async iterable #700

Open
timwis opened this issue May 12, 2020 · 1 comment
Open

Returning an async iterable #700

timwis opened this issue May 12, 2020 · 1 comment

Comments

@timwis
Copy link

timwis commented May 12, 2020

Hi! Is it possible to get an async iterable from a highland object? I'd like to be able to consume it with for-await-of if possible.

@jaidetree
Copy link
Contributor

Yes, it is possible. The trick is to transform your highland stream into a Node Readable stream which automatically implements the Symbol.asyncIterable method.

const stream = require("highland");

// Just an example to simulate async work
function delay (ms) {
  return x => stream(push => {
    setTimeout(() => {
      push(null, x);
      push(null, stream.nil);
    }, ms);
  });
}

const source = stream([ 1, 2, 3 ])
  .flatMap(delay(1000));

(async () => {
  for await (let value of source.toNodeStream({ objectMode: true })) {
    console.log(value);
  }
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants