Promise Butler
The Promise Butler is a versatile JavaScript package designed to manage the execution of promises in various ways. With this package, you can control whether promises are executed sequentially, in batches, or in a pipelined manner. This flexibility allows you to optimize the handling of asynchronous operations in your applications. The promise manager expects an array of callbacks which is expected to return a promise, with which it takes care of the promise execution.
Installation
Bundled File
CDN link (loading bundled file externally)
https://cdn.jsdelivr.net/gh/homeboy445/promiseManager/release/main.min.js
NPM Package
Install it via npm i promise-butler
(https://www.npmjs.com/package/promise-butler).
Code Walkthrough
Supported import modes
- Imports via ESM mode using
import/export
. - Imports via Common JS using
require
(ideal for NodeJS). - The bundled lib file is loaded as UMD which instantiates the package globally.
Below is for the usage via CDN & as a package.
Tip: You can pass { debugMode: true }
to getModeObject() as a parameter for enabling logging of the promises execution sequence and their results.
SEQUENTIAL MODE
- The executor callback will accept no params.
via Bundled file
const promiseArray = [() => Promise.resolve(), () => Promise.resolve()]; // Store the callbacks in the array which would return the promise to be awaited!
const { getModeObject } = promiseButler;
const promiseExecutorCallback = getModeObject().SEQUENTIAL();
await promiseExecutorCallback(promiseArray);
via ES6 imports
import { getModeObject } from "promise-butler";
const promiseArray = [() => Promise.resolve(), () => Promise.resolve()];
const promiseExecutorCallback = getModeObject().SEQUENTIAL();
await promiseExecutorCallback(promiseArray);
BATCHING MODE
- The execute callback will accept two params, namely:-
- batchSize (1st positional param): The number of batches the promise array should be divided into.
- batchWiseCallback (2nd positional param): The callback that is to be executed as soon a batch gets completed.
via Bundled file
const promiseArray = [() => Promise.resolve(), () => Promise.resolve()]; // Store the callbacks in the array which would return the promise to be awaited!
const { getModeObject } = promiseButler;
const promiseExecutorCallback = getModeObject().BATCHED(6 /*batchSize*/, () => console.log("a batch got completed!") /*batchWiseCallback*/);
await promiseExecutorCallback(promiseArray);
via ES6 imports
import { getModeObject } from "promise-butler";
const promiseArray = [() => Promise.resolve(), () => Promise.resolve()];
const promiseExecutorCallback = getModeObject().BATCHED(6 /*batchSize*/, () => console.log("a batch got completed!") /*batchWiseCallback*/);
await promiseExecutorCallback(promiseArray);
PIPELINING MODE
- The execute callback will accept on param, namely:-
- slotSize (1st positional param): The number of slots the promises should be allocated to.
via Bundled file
const promiseArray = [() => Promise.resolve(), () => Promise.resolve()]; // Store the callbacks in the array which would return the promise to be awaited!
const { getModeObject } = promiseButler;
const promiseExecutorCallback = getModeObject().PIPELINING(6 /*slotSize*/);
await promiseExecutorCallback(promiseArray);
via ES6 import
import { getModeObject } from "promise-butler";
const promiseArray = [() => Promise.resolve(), () => Promise.resolve()]; // Store the callbacks in the array which would return the promise to be awaited!
const promiseExecutorCallback = getModeObject().PIPELINING(6 /*slotSize*/);
await promiseExecutorCallback(promiseArray);
Modes
FETCH_MODES.SEQUENTIAL: Promises are executed sequentially, one after another.
FETCH_MODES.BATCHED: Promises are executed in batches, allowing for concurrent execution within each batch. A batch will consist of any non-zero number and the promises will be awaited batch-wise, for illustration: Promise.all(batch1).then(() => Promise.all(batch2))
FETCH_MODES.PIPELINING: Promises will be executed in a slot-wise manner i.e. promises will be assigned to certain slots and the rest of the promises will be executed as soon as the slots get free - leading to proper resource utilization. This will ultimately also lead to promises being executed in a PIPELINED fashion.
Issues
- If any issue is there in any of the modes, please feel free to report it by creating a ticket.