Running Two Async Iterators Concurrently in Effect Typescript

Is this the preferred way of running two async iterators at the same time? I played with acquireUseRelease but it would block the other from running.

    const program = Effect.gen(function* () {
      yield* Effect.sleep(0);

      const bybit = yield* Effect.acquireRelease(
        Effect.tryPromise(() => bybitClient.events.subscribe({})),
        release("Bybit released")
      );

      const saxo = yield* Effect.acquireRelease(
        Effect.tryPromise(() => saxoClient.events.subscribe({})),
        release("Saxo released")
      );

      yield* Stream.merge(toStream(bybit), toStream(saxo)).pipe(
        Stream.runForEach((e) => Effect.sync(() => toastEvent(e)))
      );

      function release(message: string) {
        return (iterator: AsyncIterator<TradeEvent>) =>
          Effect.all([
            Console.log(message),
            Effect.sync(() => iterator.return?.()),
          ]);
      }

      function toStream(iterable: AsyncIterable<TradeEvent>) {
        return Stream.fromAsyncIterable(iterable, (e) => new Error(String(e)));
      }
    }).pipe(Effect.scoped);
Was this page helpful?