-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 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,18 @@ | ||
--- | ||
title: Concurrency | ||
sidebar_label: Concurrency | ||
--- | ||
|
||
While you have a simple `Future.all` to run all futures in parallel (like `Promise.all` does), you might want to limit the concurrency at which you execute operations. | ||
|
||
Using `Future.concurrent`, you can specify the maximum concurrency for your array of operations. | ||
|
||
```ts | ||
Future.concurrent( | ||
userIds.map((userId) => { | ||
// notice we return a function | ||
return () => getUserById(userId); | ||
}), | ||
{ concurrency: 10 }, | ||
); | ||
``` |
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,31 @@ | ||
--- | ||
title: Retry | ||
sidebar_label: Retry | ||
--- | ||
|
||
When some operations can fail, you might want to implement a retry logic. | ||
|
||
## Retry with maximum attempts | ||
|
||
If `getUserById` outputs a `Result.Ok` value, the future resolves, if it outputs a `Result.Error`, it re-executes `getUserById`. | ||
|
||
```ts | ||
// retry immediately after failure | ||
Future.retry(() => getUserById(userId), { max: 3 }); | ||
// Future<Result<...>> | ||
``` | ||
|
||
## Rety with delay | ||
|
||
The function you pass `Future.retry` takes an `attempt` parameter, which is the current number of attempts. The count starts at `0`. | ||
|
||
```ts | ||
// adding delay | ||
Future.retry( | ||
(attempt) => { | ||
return Future.wait(attempt * 100).flatMap(() => getUserById(userId)); | ||
}, | ||
{ max: 10 }, | ||
); | ||
// Future<Result<...>> | ||
``` |
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