class F1Error {
readonly _tag = "F1Error"
}
class F2Error {
readonly _tag = "F2Error"
}
function f1(n: number) {
return pipe(
Effect.logInfo(`Starting f1: ${n}`),
Effect.flatMap(() =>
Effect.promise(() => (Math.random() > 0.5 ? Promise.reject(new F1Error()) : Promise.resolve(n * 2))),
),
Effect.tap((res) => Effect.logInfo(`Ending f1: ${n} => ${res}`)),
)
}
function f2(input: number) {
return pipe(
Effect.logInfo(`Starting f2: ${input}`),
Effect.flatMap(() =>
Effect.promise(() =>
Math.random() > 0.5 ? Promise.reject(new F2Error()) : Promise.resolve(input.toLocaleString()),
),
),
Effect.tap((res) => Effect.logInfo(`Ending f2: ${input} => ${res}`)),
)
}
const program = pipe(
f1(1),
Effect.flatMap(f2),
Effect.catchTags({
F1Error: () => Effect.logError("F1 failed"),
F2Error: () => Effect.logError("F2 failed"),
}),
)
Effect.runPromise(program)
class F1Error {
readonly _tag = "F1Error"
}
class F2Error {
readonly _tag = "F2Error"
}
function f1(n: number) {
return pipe(
Effect.logInfo(`Starting f1: ${n}`),
Effect.flatMap(() =>
Effect.promise(() => (Math.random() > 0.5 ? Promise.reject(new F1Error()) : Promise.resolve(n * 2))),
),
Effect.tap((res) => Effect.logInfo(`Ending f1: ${n} => ${res}`)),
)
}
function f2(input: number) {
return pipe(
Effect.logInfo(`Starting f2: ${input}`),
Effect.flatMap(() =>
Effect.promise(() =>
Math.random() > 0.5 ? Promise.reject(new F2Error()) : Promise.resolve(input.toLocaleString()),
),
),
Effect.tap((res) => Effect.logInfo(`Ending f2: ${input} => ${res}`)),
)
}
const program = pipe(
f1(1),
Effect.flatMap(f2),
Effect.catchTags({
F1Error: () => Effect.logError("F1 failed"),
F2Error: () => Effect.logError("F2 failed"),
}),
)
Effect.runPromise(program)