Asynchronaut - Navigate the Cosmos of Async Programming Effortlessly.
npm install asynchronautAsynchronaut is a powerful utility library for handling JavaScript promises like a pro. Simplify your async workflows with easy-to-use functions like runSequentially, runParallel, and retry and many more.
More to be added soon.
```
npm install asynchronaut
Import the functions you need:
`javascript`
const { runSequentially, runParallel } = require("asynchronaut");
Or, if you're using ESM:
`javascript
import { runSequentially, runParallel } from "asynchronaut";
// Sample tasks
const tasks = [
() => new Promise((res) => setTimeout(() => res("Task 1 Complete"), 1000)),
() => new Promise((res) => setTimeout(() => res("Task 2 Complete"), 500)),
() => new Promise((res) => setTimeout(() => res("Task 3 Complete"), 300)),
];
(async () => {
try {
const results = await runSequentially(tasks);
console.log(results);
// Output: ["Task 1 Complete", "Task 2 Complete", "Task 3 Complete"]
} catch (error) {
console.error(error.message);
}
})();
(async () => {
try {
const results = await runParallel(tasks);
console.log(results);
// Output: ["Task 1 Complete", "Task 2 Complete", "Task 3 Complete"]
} catch (error) {
console.error(error.message);
}
})();
`
For usage details, checkout src/test/
- Executes an array of async tasks one after another in sequence.
- Returns a promise that resolves with an array of results.
- Executes an array of async tasks simultaneously.
- Returns a promise that resolves with an array of results when all tasks are completed.
- Retries a provided async function a specified number of times until it succeeds or retries are exhausted.
- Returns a promise that resolves with the successful result or rejects if all attempts fail.
- Executes a given asynchronous task with a specified timeout limit. If the task does not complete within the time limit, the promise is rejected.
- Returns a Promise that resolves with the task's result or rejects with a timeout error.
- Delays the execution of async task until after a specified wait time.
- Returns a Promise that resolves with the task's result or rejects with a timeout error.
- Throttles an asynchronous task function that returns a Promise, ensuring it is not executed more than once in the specified delay period.
- Processes an array of async tasks in batches of given size.
- A possible use case can be, "How can I run 100 API calls but only 5 at a time to avoid overloading the server?" -> Use asynchronaut's batchPromises() function.
```
npm test
MIT
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Asynchronaut helps you navigate the cosmos of asynchronous JavaScript with ease and elegance!