Provides a way to write effectful code using generator functions, simplifying
control flow and error handling.
When to Use
Effect.gen allows you to write code that looks and behaves like synchronous
code, but it can handle asynchronous tasks, errors, and complex control flow
(like loops and conditions). It helps make asynchronous code more readable
and easier to manage.
The generator functions work similarly to async/await but with more
explicit control over the execution of effects. You can yield* values from
effects and return the final result at the end.
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
Handles all errors in an effect by providing a fallback effect.
Details
This function catches any errors that may occur during the execution of an
effect and allows you to handle them by specifying a fallback effect. This
ensures that the program continues without failing by recovering from errors
using the provided fallback logic.
Note: This function only handles recoverable errors. It will not recover
from unrecoverable defects.
Example (Providing Recovery Logic for Recoverable Errors)
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
Catches and handles specific errors by their _tag field, which is used as a
discriminator.
When to Use
catchTag is useful when your errors are tagged with a readonly _tag field
that identifies the error type. You can use this function to handle specific
error types by matching the _tag value. This allows for precise error
handling, ensuring that only specific errors are caught and handled.
The error type must have a readonly _tag field to use catchTag. This
field is used to identify and match errors.
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
Handles all errors in an effect by providing a fallback effect.
Details
This function catches any errors that may occur during the execution of an
effect and allows you to handle them by specifying a fallback effect. This
ensures that the program continues without failing by recovering from errors
using the provided fallback logic.
Note: This function only handles recoverable errors. It will not recover
from unrecoverable defects.
Example (Providing Recovery Logic for Recoverable Errors)
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
}) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Executes two effects sequentially, returning the result of the second effect
while ignoring the result of the first.
Details
This function allows you to run two effects in sequence, keeping the result
of the second effect and discarding the result of the first. By default, the
two effects are executed sequentially. If you need them to run concurrently,
you can pass the { concurrent: true } option.
The first effect will always be executed, even though its result is ignored.
This makes it useful for scenarios where the first effect is needed for its
side effects, but only the result of the second effect is important.
When to Use
Use this function when you are only interested in the result of the second
effect but still need to run the first effect for its side effects, such as
initialization or setup tasks.
Executes an effect synchronously, running it immediately and returning the
result.
Details
This function evaluates the provided effect synchronously, returning its
result directly. It is ideal for effects that do not fail or include
asynchronous operations. If the effect does fail or involves async tasks, it
will throw an error. Execution stops at the point of failure or asynchronous
operation, making it unsuitable for effects that require asynchronous
handling.
Important: Attempting to run effects that involve asynchronous operations
or failures will result in exceptions being thrown, so use this function with
care for purely synchronous and error-free effects.
When to Use
Use this function when:
You are sure that the effect will not fail or involve asynchronous
operations.
You need a direct, synchronous result from the effect.
You are working within a context where asynchronous effects are not
allowed.
Avoid using this function for effects that can fail or require asynchronous
handling. For such cases, consider using
runPromise
or
runSyncExit
.
Example (Synchronous Logging)
import { Effect } from"effect"
constprogram= Effect.sync(() => {
console.log("Hello, World!")
return1
})
constresult= Effect.runSync(program)
// Output: Hello, World!
console.log(result)
// Output: 1
Example (Incorrect Usage with Failing or Async Effects)
import { Effect } from"effect"
try {
// Attempt to run an effect that fails
Effect.runSync(Effect.fail("my error"))
} catch (e) {
console.error(e)
}
// Output:
// (FiberFailure) Error: my error
try {
// Attempt to run an effect that involves async work
// (FiberFailure) AsyncFiberException: Fiber #0 cannot be resolved synchronously. This is caused by using runSync on an effect that performs async work
@see ― runSyncExit for a version that returns an Exit type instead of
throwing an error.
@since ― 2.0.0
runSync(
constprogram:Effect.Effect<void, never, never>
program);
This will print messages to a mixture of stdout and stderr.
Terminal window
[stdout] starting
[stdout] trying first password
[stderr] Password must be at least 8 characters
[stdout] trying second password
[stderr] Password must not be a common password
[stdout] trying third password
[stdout] third password OK
[stdout] done
Console.error can be implemented in the same way that was shown in Console.log (44)
.