Lesson 05 of 6 in Effect, one function at a time

Stop waiting with Effect.timeout

Stop waiting after a deadline.

A call to a slow service can hang. Effect.timeout gives it a deadline, so the caller stops waiting instead of blocking forever.

import { Duration, Effect } from "effect";

const slowLookup = Effect.promise(
	() =>
		new Promise<string>((resolve) => setTimeout(() => resolve("found"), 5000)),
);

export const lookupWithDeadline = slowLookup.pipe(
	Effect.timeout(Duration.seconds(2)),
);

Three things to notice:

Pick the deadline from what the caller can afford to wait, not from the slowest case you happen to have seen.