Skip to content

A Typescript utility that allows you to handle a sequence of tasks(function or promise) in a defined order with support for error handling.

License

Notifications You must be signed in to change notification settings

xchristopherhayes/request-sequencer

Repository files navigation

RequestSequencer - Typescript Utility for Sequential Task Execution

npm

Overview

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.

Features

  • 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.

Installation

npm install sequential-request

Usage

Create a Sequential Request Instance

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,
  };
});

Adding Tasks

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"));

Handling Errors

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,
  }));

Foreach Loop

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));

Ending the Sequence

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,
  }));

Guaranteed Error Handling

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");

License

MIT License

About

A Typescript utility that allows you to handle a sequence of tasks(function or promise) in a defined order with support for error handling.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published