-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibers.ts
More file actions
34 lines (28 loc) · 898 Bytes
/
fibers.ts
File metadata and controls
34 lines (28 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Effect, Array, Data, Fiber } from "effect";
// const hello = Effect.gen(function* () {
// yield* Effect.log("before sleep");
// yield* Effect.fork(Effect.log("after sleep"));
// return "hello";
// });
//
class CustomError extends Data.TaggedError("CustomError") {}
const hello = Effect.gen(function* () {
yield* Effect.log("beforeSleep").pipe(Effect.fork);
yield* Effect.log("parent finishing");
return "hello";
});
const slow = Effect.gen(function* () {
yield* Effect.sleep("2 seconds");
return yield* new CustomError();
});
const prog = Effect.gen(function* () {
const fiber = yield* Effect.fork(slow);
const exit = yield* Fiber.join(fiber);
// exit is Exit<number, CustomError>
if (exit._tag === "Success") {
yield* Effect.log(`Got: ${exit.value}`);
} else {
yield* Effect.log("Failed, but no error propagated");
}
});
Effect.runPromise(prog);