Effect.ts: Absence as First-Class

ConcurrencyLesson 10 of 51

10. HOW #5: Parallel Resolution

Resolve multiple absences at once

Code Example
const getUser = Effect.sleep("100ms")
  .pipe(Effect.as({ name: "Alice" }));

const getPosts = Effect.sleep("150ms")
  .pipe(Effect.as(["Post 1", "Post 2"]));

const getComments = Effect.sleep("120ms")
  .pipe(Effect.as(["Comment A"]));

// HOW: Resolve all three in parallel
const all = Effect.all([
  getUser,
  getPosts,
  getComments
]);

// Total time: ~150ms (not 370ms!)
Interactive Example
const getUser = Effect.sleep('100 millis').pipe(Effect.as({ name: 'Alice' }));
const getPosts = Effect.sleep('150 millis').pipe(Effect.as(['Post 1', 'Post 2']));
const getComments = Effect.sleep('120 millis').pipe(Effect.as(['Comment A']));
const start = Date.now();
const [user, posts, comments] = await Effect.runPromise(
      Effect.all([getUser, getPosts, getComments])
);
const elapsed = Date.now() - start;
return `Parallel execution!

User: ${user.name} (100ms)
Posts: ${posts.length} posts (150ms)
Comments: ${comments.length} comment (120ms)

Total time: ${elapsed}ms (not 370ms!)

All resolved concurrently!`;
Explanation

Parallel: resolving multiple absences concurrently.

Traditional approach: fetch sequentially, each waits for the previous. Effect types unavailability with concurrent resolution:

  • User is unavailable
  • Posts are unavailable
  • Comments are unavailable
  • No assumptions about any being available first

We describe: "All three are absent, resolve them all at once!"


Part 10 of 51 in the Effect.ts Absence Modeling series