@schamane/serial-exec
v2.0.2
Published
Serial execution of asynchronouse functions for javascript
Downloads
16
Readme
serialExec
Serial execution of asynchronouse functions for javascript with zero dependancies.
Use this if serial execution of anychronouse code should be implemented.
Let say you will execute HTTP request with different values not in parallel, but waiting on request is done, and also be abble to break execution.
Package can be used als commonjs or esm module
Breaking API changes for v2
Note
We made api changes to package since v1.0, please use "single" tag on npm to use older version
npm install @schamane/serial-exec:single
Usage
There are two methods that can be used to serial execution of promises
- all
- overParams
async all(list: wrapedFunction[]): Promise<T[]>;
async overParams(params: any[], fn: Function): Promise<T[]>;
Usage example for all
import { all, useSerialExec } from '@schamane/serial-exec';
const delay = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const asyncFn = useSerialExec(async (breakFn, ms) => {
if (ms > 300) {
return breakFn();
}
console.log(`fn ${ms} start`);
await delay(ms);
console.log(`fn ${ms} done`);
return ms + 1;
});
await all([asyncFn(100), asyncFn(200), asyncFn(400), asyncFn(1000)]);
Usage example for overParams
import { overParams } from '@schamane/serial-exec';
const delay = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const asyncFn = async (ms, breakFn) => {
if (ms > 300) {
return breakFn();
}
console.log(`fn ${ms} start`);
await delay(ms);
console.log(`fn ${ms} done`);
return ms + 1;
};
await overParams([100, 200, 400, 1000], asyncFn);