Skip to content

Commit

Permalink
Add docs on retry & concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodyowl committed Oct 27, 2024
1 parent e52f4fb commit a5ab74e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/docs/concurrency.md
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 },
);
```
31 changes: 31 additions & 0 deletions docs/docs/retry.md
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<...>>
```
2 changes: 2 additions & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ module.exports = {
"form-validation",
"nested-optional-values",
"cancellable-request",
"retry",
"concurrency",
],
},
{
Expand Down

0 comments on commit a5ab74e

Please sign in to comment.