Sequential combinators such as Effect.zip, Effect.all and Effect.forEach have a “fail fast” policy when it comes to error management. This means that they stop and return immediately when they encounter the first error.
Here’s an example using Effect.zip, which stops at the first failure and only shows the first error:
Example (Fail Fast with Effect.zip)
The Effect.forEach function behaves similarly. It applies an effectful operation to each element in a collection, but will stop when it hits the first error:
Example (Fail Fast with Effect.forEach)
However, there are cases where you may want to collect all errors rather than fail fast. In these situations, you can use functions that accumulate both successes and errors.
validate
The Effect.validate function is similar to Effect.zip, but it continues combining effects even after encountering errors, accumulating both successes and failures.
Example (Validating and Collecting Errors)
validateAll
The Effect.validateAll function is similar to the Effect.forEach function. It transforms all elements of a collection using the provided effectful operation, but it collects all errors in the error channel, as well as the success values in the success channel.
validateFirst
The Effect.validateFirst function is similar to Effect.validateAll but it returns the first successful result, or all errors if none succeed.
Example (Returning the First Success)
Notice that Effect.validateFirst returns a single number as the success type, rather than an array of results like Effect.validateAll.
partition
The Effect.partition function processes an iterable and applies an effectful function to each element. It returns a tuple, where the first part contains all the failures, and the second part contains all the successes.
Example (Partitioning Successes and Failures)
This operator is an unexceptional effect, meaning the error channel type is never. Failures are collected without stopping the effect, so the entire operation completes and returns both errors and successes.