Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(async module): add search terms and popular use cases #211

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions docs/async/all.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ const { user } = await _.all({
message: slack.customerSuccessChannel.sendMessage(...)
})
```

### Search terms

- Often referred to as `all`, `promiseAll`, or `aggregatePromiseAll`

### Popular use cases

- Use this function to concurrently execute multiple asynchronous tasks, ensuring that all results are gathered, while handling any potential errors in a clean and organized manner.
- Utilize it in a scenario where you need to create multiple resources (e.g., users, buckets, channels) at the same time from different APIs, and want to manage both successful responses and potential errors effectively.
- Leverage it for parallel execution of API calls in a batch process, where each promise may fail independently, but you still want to collect all results or errors related to these calls.
10 changes: 10 additions & 0 deletions docs/async/defer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ await _.defer(async register => {
await executeTest(org, user)
})
```

### Search terms

- Commonly referenced as `defer`, `cleanup`, or `ensure`

### Popular use cases

- Using `defer` for resource management, such as automatically closing file handles or database connections in a controlled manner after operations complete, ensuring that all cleanup code runs regardless of success or failure.
- Implementing deferred execution for asynchronous tasks, allowing multiple callbacks to be registered that handle cleanup logic, which is especially useful in error-prone operations like network requests or file manipulations.
- Facilitating error handling in complex code flows by automatically executing error handling callbacks, improving code maintainability and readability while simplifying structured cleanup of resources.
10 changes: 10 additions & 0 deletions docs/async/guard.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ You can choose to guard only specific errors too
const isInvalidUserError = (err: any) => err.code === 'INVALID_ID'
const user = (await guard(fetchUser, isInvalidUserError)) ?? DEFAULT_USER
```

### Search terms

- Often called `tryAsync`, `attemptAsync`, or `safeCall` in other libraries.

### Popular use cases

- Safely executing asynchronous functions in environments where exceptions are undesirable, replacing potential errors with `undefined`.
- Wrapping network requests or database calls to simplify error handling within complex applications.
- Utilizing a guard clause for fallback mechanisms when integrating third-party APIs that may be unreliable or sporadically fail.
10 changes: 10 additions & 0 deletions docs/async/map.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ const users = await _.map(userIds, async userId => {
return await api.users.find(userId)
})
```

### Search terms

- Often called `asyncMap`, `mapAsync`, or `promiseMap` in other libraries.

### Popular use cases

- Parallel data fetching: Use `map` to iterate over an array of URLs, fetching data from each asynchronously and collecting the results.
- Concurrent processing: Utilize `map` to apply a heavy computation asynchronously on an array of items, such as hashing passwords or processing images.
- Batch processing: Employ `map` to handle bulk updates or inserts in a database where each operation needs to be done asynchronously.
10 changes: 10 additions & 0 deletions docs/async/parallel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ console.log(err) // => AggregateError
console.log(err.errors) // => [Error, Error, Error]
console.log(err.errors[1].message) // => No, I don't want to find user 2
```

### Search terms

- Often called `parallelize`, `concurrent`, or `asyncMap` in other libraries.

### Popular use cases

- Running multiple API calls concurrently and aggregating results while handling errors in bulk.
- Processing a large set of data items (e.g., files, database records) in parallel with controlled concurrency.
- Performing parallel execution of tasks where the order of execution results does not matter but managing error propagation is necessary.
10 changes: 10 additions & 0 deletions docs/async/reduce.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ const users = await _.reduce(
{},
)
```

### Search terms

- Often called `asyncReduce`, `asyncFold`, or `reduceAsync` in other libraries.

### Popular use cases

- Aggregating results from an array of promises where each promise's result is dependent on the previous result.
- Performing complex, sequential data transformations on large datasets within a controlled asynchronous context.
- Sequentially processing a list of tasks where each task needs the result of the previously resolved task, especially in data pipelines or batch processing scenarios.
10 changes: 10 additions & 0 deletions docs/async/retry.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ await _.retry({ times: 2, delay: 1000 }, api.users.list)
// exponential backoff
await _.retry({ backoff: i => 10 ** i }, api.users.list)
```

### Search Terms

- Often called `retry`, `retryAsync`, or `retryOperation` in other libraries.

### Popular Use Cases

- Retrying API calls or network requests that may intermittently fail.
- Implementing resilient calls to third-party services where temporary downtime or hiccups may occur.
- Handling operations that may fail due to race conditions or concurrency issues, like database queries or file system operations.
11 changes: 11 additions & 0 deletions docs/async/sleep.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ import * as _ from 'radashi'

await _.sleep(2000) // => waits 2 seconds
```

### Search terms

- Often called `delay`, `timeout`, `wait`, `pause`, or `sleep` in other libraries.

### Popular use cases

- Delay the execution of a function or a block of code for a specified duration.
- Creating timed intervals between retry attempts in asynchronous operations or network requests.
- Simulating latency or downtime in testing environments to validate timeout handling in applications.
- Throttling or debouncing actions in user interfaces to enhance performance and usability.
10 changes: 10 additions & 0 deletions docs/async/tryit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ const findUser = _.tryit(api.users.find)

const [err, user] = await findUser(userId)
```

### Search terms

- Often called `tryCatch`, `to`, or `attempt` in other libraries.

### Popular use cases

- Handling asynchronous operations without explicit try-catch blocks, ensuring promises are safely caught.
- Simplifying error handling in promise-based code by returning `[Error, result]` tuples, streamlining control flow.
- Wrapping synchronous and asynchronous functions to provide consistent error-first handling, useful in complex asynchronous functions and APIs.
10 changes: 10 additions & 0 deletions docs/async/withResolvers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ const { resolve, reject, promise } = withResolvers()

resolve(42)
```

### Search terms

- Often called `deferred`, `promiseWithDeferred`, or `promiseResolver` in other libraries.

### Popular use cases

- Handling asynchronous operations in a more controlled manner where you need manual resolution or rejection of a promise, often used for testing or mock implementation.
- Integrating with APIs or libraries that do not natively support promises by enabling conversion from callback-style to promise-style code.
- Coordinating multiple asynchronous operations where one promise's resolution should trigger others, enabling complex workflows or event handling.
Loading