Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/duzun/asyncseries.js
Asynchronously process a list of items consecutively
https://github.com/duzun/asyncseries.js
array async async-serial asynchronous esm foreach iteratee map promise reduce reducer series
Last synced: 20 days ago
JSON representation
Asynchronously process a list of items consecutively
- Host: GitHub
- URL: https://github.com/duzun/asyncseries.js
- Owner: duzun
- License: mit
- Created: 2020-01-17T12:27:21.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-02T20:23:38.000Z (almost 2 years ago)
- Last Synced: 2024-04-24T13:17:50.577Z (8 months ago)
- Topics: array, async, async-serial, asynchronous, esm, foreach, iteratee, map, promise, reduce, reducer, series
- Language: JavaScript
- Size: 352 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# asyncSeries.js [![Build Status](https://app.travis-ci.com/duzun/asyncSeries.js.svg?branch=master)](https://app.travis-ci.com/duzun/asyncSeries.js) [![codecov](https://codecov.io/gh/duzun/asyncSeries.js/branch/master/graph/badge.svg)](https://codecov.io/gh/duzun/asyncSeries.js)
Asynchronously process a list of items consecutively.
Even though the processing is asynchronous, it is never done in parallel.
## Install
```sh
npm i -S @duzun/async-series
```## Import or require
This library can be included either as an ESM or UMD.
#### ESM
```js
import {
forEach as eachSeries,
map as mapSeries,
reduce as reduceSeries
} from '@duzun/async-series';
```#### CommonJS
```js
const {
forEach: eachSeries,
map : mapSeries,
reduce : reduceSeries,
} = require('@duzun/async-series');
```#### AMD
```js
require('https://unpkg.com/@duzun/async-series', (asyncSeries) => {
const {
forEach: eachSeries,
map : mapSeries,
reduce : reduceSeries,
} = asyncSeries;// ...
});
```#### Browser
```html
const {
forEach: eachSeries,
map : mapSeries,
reduce : reduceSeries,
} = asyncSeries;```
## Usage by example
### forEach(array, iteratee)
##### Where `[async ]iteratee(value, index, array)`Process file contents of a directory, asynchronously & consecutively.
```js
const fs = require('mz/fs');
const { forEach: eachSeries } = require('@duzun/async-series');(async () => {
let files = await fs.readdir('.');
let results = await eachSeries(files, async (filename, idx) => {
if((await fs.stat(filename)).isDirectory()) return;
let contents = await fs.readFile(filename);// Do some heavy processing here...
});
})();
```### map(array, iteratee[, firstValue])
##### Where `[async ]iteratee(value, index, array)`Get file contents of a directory, asynchronously & consecutively;
```js
const fs = require('mz/fs');
const { map: mapSeries } = require('@duzun/async-series');(async () => {
let files = await fs.readdir('.');
let contents = await mapSeries(files, async (filename, idx) => {
if((await fs.stat(filename)).isDirectory()) return;
return fs.readFile(filename, 'utf8');
});
contents = contents.filter((x) => x);
})();
```### reduce(array, reducer[, initialValue])
##### Where `[async ]reducer(accumulator, currentValue, index, array)`Calculate file sizes in a directory, asynchronously & consecutively.
```js
const fs = require('mz/fs');
const { reduce: reduceSeries } = require('@duzun/async-series');(async () => {
let files = await fs.readdir('.');
let size = await reduceSeries(files, async (size, filename, idx) => {
let stats = await fs.stat(filename);
if(stats.isDirectory()) return size;
return size + stats.size;
}, 0);console.log('size: ', size);
})();```
## Why?
Why not? :)
I know, there is a great library [Async.js](https://caolan.github.io/async/), which already has [eachSeries](https://caolan.github.io/async/v3/docs.html#eachSeries), [mapSeries](https://caolan.github.io/async/v3/docs.html#mapSeries) and [reduce](https://caolan.github.io/async/v3/docs.html#reduce) and many other functions.
This library is **< 900 bytes** minified, and **< 500 bytes** when also gzipped.
And if you include it with [rollup.js](https://rollupjs.org/), you only get the function you've imported.