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

Support async pipe #3

Closed
4513ECHO opened this issue Aug 22, 2024 · 3 comments · Fixed by #4
Closed

Support async pipe #3

4513ECHO opened this issue Aug 22, 2024 · 3 comments · Fixed by #4
Assignees
Labels
enhancement New feature or request

Comments

@4513ECHO
Copy link

Currently piping async function to sync function is impossible.

const syncFn = (x) => { ... }
const asyncFn = async (x) => { ... }

await pipe(
  [],
  asyncFn,
  syncFn, // Error because the argument is `Promise<T>`
)

We need jsr:@core/pipe/async or something to support it.

@lambdalisue lambdalisue added the enhancement New feature or request label Aug 22, 2024
@lambdalisue lambdalisue self-assigned this Aug 22, 2024
@lambdalisue
Copy link
Member

lambdalisue commented Aug 22, 2024

@4513ECHO Could you try pipe in async module of https://jsr.io/@core/[email protected]

@4513ECHO
Copy link
Author

It works fine. Thanks!

@lambdalisue
Copy link
Member

📝

This was required to combine the following two pipe

import { pipe } from "jsr:@core/pipe";
import { pipe as pipeAsync } from "jsr:@core/pipe/async";
import { toAsyncIterable } from "jsr:@core/iterutil/async/to-async-iterable";
import { map } from "jsr:@core/iterutil/pipe/async/map";

// We must resolve the promise before we can use the value
// in the next pipe function. So that we cannot combine
// the following two pipes into one.
const fst = await pipe(
  toAsyncIterable([1, 2, 3]),
  map((x) => x * 2),
  (v) => Array.fromAsync(v),
);
const snd = pipe(
  fst,
  (v) => v.join(""),
  (v) => v.repeat(2),
);
console.log(snd);

// If we use `pipe/async` instead, we can combine the two pipes
const trd = await pipeAsync(
  toAsyncIterable([1, 2, 3]),
  map((x) => x * 2),
  (v) => Array.fromAsync(v),
  (v) => v.join(""),
  (v) => v.repeat(2),
);
console.log(trd);

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

Successfully merging a pull request may close this issue.

2 participants