Hyperlinkv0.8.0-beta.28

Effect

Effect.zipWithconsteffect/Effect.ts:2602
<A2, E2, R2, A, B>(
  that: Effect<A2, E2, R2>,
  f: (a: A, b: A2) => B,
  options?: { readonly concurrent?: boolean | undefined }
): <E, R>(self: Effect<A, E, R>) => Effect<B, E2 | E, R2 | R>
<A, E, R, A2, E2, R2, B>(
  self: Effect<A, E, R>,
  that: Effect<A2, E2, R2>,
  f: (a: A, b: A2) => B,
  options?: { readonly concurrent?: boolean | undefined }
): Effect<B, E2 | E, R2 | R>

Combines two effects sequentially and applies a function to their results to produce a single value.

When to use

Use when you need to run two effects sequentially and combine their results with a function instead of keeping the results as a tuple.

Details

Concurrency:

By default, the effects are run sequentially. To execute them concurrently, use the { concurrent: true } option.

Example (Combining two success values with a function)

import { Effect } from "effect"

const task1 = Effect.succeed(1).pipe(
  Effect.delay("200 millis"),
  Effect.tap(Effect.log("task1 done"))
)
const task2 = Effect.succeed("hello").pipe(
  Effect.delay("100 millis"),
  Effect.tap(Effect.log("task2 done"))
)

const task3 = Effect.zipWith(
  task1,
  task2,
  // Combines results into a single value
  (number, string) => number + string.length
)

Effect.runPromise(task3).then(console.log)
// Output:
// timestamp=... level=INFO fiber=#3 message="task1 done"
// timestamp=... level=INFO fiber=#2 message="task2 done"
// 6
zipping
Source effect/Effect.ts:260213 lines
export const zipWith: {
  <A2, E2, R2, A, B>(
    that: Effect<A2, E2, R2>,
    f: (a: A, b: A2) => B,
    options?: { readonly concurrent?: boolean | undefined }
  ): <E, R>(self: Effect<A, E, R>) => Effect<B, E2 | E, R2 | R>
  <A, E, R, A2, E2, R2, B>(
    self: Effect<A, E, R>,
    that: Effect<A2, E2, R2>,
    f: (a: A, b: A2) => B,
    options?: { readonly concurrent?: boolean | undefined }
  ): Effect<B, E2 | E, R2 | R>
} = internal.zipWith
Referenced by 1 symbols