-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Effect.filterEffect* apis (#4335)
- Loading branch information
Showing
4 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
"effect": minor | ||
--- | ||
|
||
add Effect.filterEffect\* apis | ||
|
||
#### Effect.filterEffectOrElse | ||
|
||
Filters an effect with an effectful predicate, falling back to an alternative | ||
effect if the predicate fails. | ||
|
||
```ts | ||
import { Effect, pipe } from "effect" | ||
|
||
// Define a user interface | ||
interface User { | ||
readonly name: string | ||
} | ||
|
||
// Simulate an asynchronous authentication function | ||
declare const auth: () => Promise<User | null> | ||
|
||
const program = pipe( | ||
Effect.promise(() => auth()), | ||
// Use filterEffectOrElse with an effectful predicate | ||
Effect.filterEffectOrElse({ | ||
predicate: (user) => Effect.succeed(user !== null), | ||
orElse: (user) => Effect.fail(new Error(`Unauthorized user: ${user}`)) | ||
}) | ||
) | ||
``` | ||
|
||
#### Effect.filterEffectOrFail | ||
|
||
Filters an effect with an effectful predicate, failing with a custom error if the predicate fails. | ||
|
||
```ts | ||
import { Effect, pipe } from "effect" | ||
|
||
// Define a user interface | ||
interface User { | ||
readonly name: string | ||
} | ||
|
||
// Simulate an asynchronous authentication function | ||
declare const auth: () => Promise<User | null> | ||
|
||
const program = pipe( | ||
Effect.promise(() => auth()), | ||
// Use filterEffectOrFail with an effectful predicate | ||
Effect.filterEffectOrFail({ | ||
predicate: (user) => Effect.succeed(user !== null), | ||
orFailWith: (user) => Effect.fail(new Error(`Unauthorized user: ${user}`)) | ||
}) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters