What is spex?
The 'spex' npm package is a specialized library for handling advanced promise operations in JavaScript. It provides a set of tools for managing and orchestrating multiple promises, making it easier to handle complex asynchronous workflows.
What are spex's main functionalities?
Batch Processing
Batch processing allows you to execute multiple promise-returning functions in parallel and collect their results. The example demonstrates how to use the 'batch' method to run an array of tasks and handle their results.
const spex = require('spex')();
const tasks = [
() => Promise.resolve(1),
() => Promise.resolve(2),
() => Promise.resolve(3)
];
spex.batch(tasks)
.then(data => {
console.log(data); // [1, 2, 3]
})
.catch(error => {
console.error(error);
});
Sequence Processing
Sequence processing allows you to execute multiple promise-returning functions in sequence, one after another. The example demonstrates how to use the 'sequence' method to run an array of tasks sequentially and handle their results.
const spex = require('spex')();
const tasks = [
() => Promise.resolve(1),
() => Promise.resolve(2),
() => Promise.resolve(3)
];
spex.sequence(tasks)
.then(data => {
console.log(data); // [1, 2, 3]
})
.catch(error => {
console.error(error);
});
Page Processing
Page processing allows you to handle paginated data sources. The example demonstrates how to use the 'page' method to process pages of data until the source function returns null.
const spex = require('spex')();
let pageIndex = 0;
const source = (index) => {
if (index < 3) {
return Promise.resolve(index);
} else {
return null;
}
};
spex.page(source)
.then(data => {
console.log(data); // {pages: 3, total: 3}
})
.catch(error => {
console.error(error);
});
Other packages similar to spex
bluebird
Bluebird is a fully-featured promise library that provides a wide range of utilities for working with promises, including advanced control flow, collection methods, and more. Compared to spex, Bluebird offers a broader set of features but may be more complex to use for specific batch and sequence processing tasks.
async
Async is a utility module that provides straightforward, powerful functions for working with asynchronous JavaScript. It includes methods for parallel and sequential execution of tasks, similar to spex, but uses callbacks instead of promises. Async is a good choice if you prefer callback-based APIs.
p-map
p-map is a promise-based utility for mapping over iterables concurrently. It allows you to control the concurrency level, making it similar to spex's batch processing. However, p-map is more focused on mapping operations and does not provide sequence or page processing features.
Specialized Promise Extensions
batch, page, sequence - promise methods for the following patterns:
Installing
$ npm install spex
Usage
var promise = require('bluebird');
var spex = require('spex')(promise);
var spex = require('spex')(Promise);
See also: client-side usage.
API
Testing
- Clone the repository (or download, if you prefer):
$ git clone https://github.com/vitaly-t/spex
- Install the library's DEV dependencies:
$ npm install
$ npm test
- To run all tests with coverage:
$ npm run coverage
License
Copyright © 2018 Vitaly Tomilov;
Released under the MIT license.