RequestSequencer
is a Typescript utility that allows you to handle a sequence of tasks(function or promise) in a defined order with support for error handling.
- Chained Task Execution: Tasks can be chained sequentially, where each task is executed after the previous one.
- Error Handling: Each task can have its own custom error handler, and the final promise guarantees that errors can be handled gracefully.
- Foreach Support: You can iterate over arrays or resolved array values and execute a task for each item in the array.
- Default Guaranteed Error Handling: You can provide a default error handler for tasks that may not have their own error handler.
npm install sequential-request
import { RequestSequencer } from "request-sequencer";
const request = new RequestSequencer();
You can also pass a default error handler to the constructor:
const requestWithErrorHandler = new RequestSequencer((error) => {
return {
message: "Default Error Handler",
error,
};
});
You can chain tasks using the next
method. Tasks can be functions that return a promise or a direct promise value.
request
.next(Promise.resolve("Task 1"))
.next((prevResult) => Promise.resolve("Task 2"));
You can catch errors at any point in the sequence by chaining the catch
method:
request
.next(Promise.resolve("Task 1"))
.next((prevResult) => Promise.resolve("Task 2"))
.catch((error) => ({
message: "Task 2 Error Handler",
error,
}));
You can iterate over arrays of items and process them sequentially using the foreach
method.
request
.next(Promise.resolve("Task 1"))
.next((prevResult) => Promise.resolve("Task 2"))
.catch((error) => ({
message: "Task 2 Error Handler",
error,
}))
.foreach([1, 2, 3], (item) => Promise.resolve(item));
Finally, the end
method allows you to specify a callback that is called with all the results of the sequential tasks:
request
.next(Promise.resolve("Task 1"))
.next((prevResult) => Promise.resolve("Task 2"))
.catch((error) => ({
message: "Task 2 Error Handler",
error,
}))
.foreach([1, 2, 3], (item) => Promise.resolve(item))
.end(([res1, res2, res3]) => ({
result1: res1,
result2: res2,
result3: res3,
}));
You can guarantee an error handler for the entire request:
request
.next(Promise.resolve("Task 1"))
.next((prevResult) => Promise.resolve("Task 2"))
.catch((error) => ({
message: "Task 2 Error Handler",
error,
}))
.foreach([1, 2, 3], (item) => Promise.resolve(item))
.end(([res1, res2, res3]) => ({
result1: res1,
result2: res2,
result3: res3,
}))
.guarantee((error) => ({
message: "Guaranteed Error Handler",
error,
}));
You can also use the default guaranteed error handler:
guarantee("DEFAULT");
MIT License