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

[transducers] feature async generators #333

Open
ja0nz opened this issue Dec 11, 2021 · 1 comment
Open

[transducers] feature async generators #333

ja0nz opened this issue Dec 11, 2021 · 1 comment

Comments

@ja0nz
Copy link
Contributor

ja0nz commented Dec 11, 2021

As a big fan of async generators I would love to see some support from the @thi.ng/transducers package.
Conceptually they are not too far from normal iterators anyway so I hope this feature is possible.

Iterators Async iterators
Object method to provide iterator Symbol.iterator Symbol.asyncIterator
next() return value is any value Promise
to loop, use for..of for await..of

Example usage: NodeJS - simple directory recursion

import { opendir } from "node:fs/promises";
import { extname, join } from "node:path";

async function* walk(dir) {
  for await (const d of await opendir(dir)) {
    const entry = join(dir, d.name);
    if (d.isDirectory()) yield* walk(entry);
    else if (d.isFile()) yield entry;
  }
}

const collectMarkdownFiles = await transduce(filter(p => extname(p) === ".md"), push(), walk('.'))

Example usage: Alternating URL fetch

async function* getResources() {
  yield fetch('https://someurl.org')
  yield fetch('https://someFallback.org')
}

// Imperative usage
for await (const res of getResources()) {
  if (res.status === 404) continue;
  // response handling
  break;
}

// Declarative usage
const payload = await transduce(/* response handling */, push(), getResources())
@postspectacular
Copy link
Member

Just a quick one: There's a 3.5 year old branch for async transducers. I stopped with it due to widespread lack of async/await support back then. I've been meaning to revisit it for a while, just no bandwidth so far. If you're interested in taking a peek:

https://github.com/thi-ng/umbrella/blob/feature/async-transducers/packages/async/src/index.ts

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