metasync
所属分类:区块链开发
开发工具:JavaScript
文件大小:104KB
下载次数:0
上传日期:2023-01-08 13:42:18
上 传 者:
sh-1993
说明: 用于JavaScript和Node.js的异步编程库
(Asynchronous Programming Library for JavaScript & Node.js)
文件列表:
.babelrc (32, 2022-11-15)
.editorconfig (207, 2022-11-15)
.eslintignore (26, 2022-11-15)
.eslintrc.json (233, 2022-11-15)
.metadocrc (763, 2022-11-15)
.prettierignore (26, 2022-11-15)
.prettierrc (152, 2022-11-15)
AUTHORS (318, 2022-11-15)
LICENSE (1083, 2022-11-15)
doc (0, 2022-11-15)
doc\footer.md (154, 2022-11-15)
doc\header.md (2052, 2022-11-15)
lib (0, 2022-11-15)
lib\adapters.js (1826, 2022-11-15)
lib\array.js (11586, 2022-11-15)
lib\async-iterator.js (9527, 2022-11-15)
lib\collector.class.js (2773, 2022-11-15)
lib\collector.functor.js (2139, 2022-11-15)
lib\collector.js (3531, 2022-11-15)
lib\collector.prototype.js (3444, 2022-11-15)
lib\composition.js (5117, 2022-11-15)
lib\control.js (3780, 2022-11-15)
lib\do.js (865, 2022-11-15)
lib\fp.js (1718, 2022-11-15)
lib\memoize.js (2112, 2022-11-15)
lib\poolify.js (1064, 2022-11-15)
lib\poolify.opt.js (1025, 2022-11-15)
lib\poolify.symbol.js (1132, 2022-11-15)
... ...
# Asynchronous Programming Library
[![ci status](https://github.com/metarhia/metasync/workflows/Testing%20CI/badge.svg)](https://github.com/metarhia/metasync/actions?query=workflow%3A%22Testing+CI%22+branch%3Amaster)
[![snyk](https://snyk.io/test/github/metarhia/metasync/badge.svg)](https://snyk.io/test/github/metarhia/metasync)
[![npm version](https://badge.fury.io/js/metasync.svg)](https://badge.fury.io/js/metasync)
[![npm downloads/month](https://img.shields.io/npm/dm/metasync.svg)](https://www.npmjs.com/package/metasync)
[![npm downloads](https://img.shields.io/npm/dt/metasync.svg)](https://www.npmjs.com/package/metasync)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/metarhia/metasync/blob/master/LICENSE)
## Installation
```bash
$ npm install metasync
```
## Asynchronous functions composition
`metasync(fns)(data, done)`
- `fns` - array of callback-last functions, callback contranct err-first
- `data` - input data (optional)
- `done` - err-first callback
- Returns: composed callback-last / err-first function
![composition](https://cloud.githubusercontent.com/assets/4405297/16968374/1b81f160-4e17-11e6-96fa-9d7e2b422396.png)
```js
const composed = metasync([f1, f2, f3, [[f4, f5, [f6, f7], f8]], f9]);
```
- Array of functions gives sequential execution: `[f1, f2, f3]`
- Double brackets array of functions gives parallel execution: `[[f1, f2, f3]]`
_Example:_
```js
const metasync = require('metasync');
const fs = require('fs');
// Data collector (collect keys by count)
const dc = metasync.collect(4);
dc.pick('user', { name: 'Marcus Aurelius' });
fs.readFile('HISTORY.md', (err, data) => dc.collect('history', err, data));
dc.take('readme', fs.readFile, 'README.md');
setTimeout(() => dc.pick('timer', { date: new Date() }), 1000);
// Key collector (collect certain keys by names)
const kc = metasync
.collect(['user', 'history', 'readme', 'timer'])
.timeout(2000)
.distinct()
.done((err, data) => console.log(data));
kc.pick('user', { name: 'Marcus Aurelius' });
kc.take('history', fs.readFile, 'HISTORY.md');
kc.take('readme', fs.readFile, 'README.md');
setTimeout(() => kc.pick('timer', { date: new Date() }), 1000);
```
## API
### callbackify(fn)
- `fn`: [`
`][function] promise-returning function
_Returns:_ [``][function]
Convert Promise-returning to callback-last / error-first contract
### asyncify(fn)
- `fn`: [``][function] regular synchronous function
_Returns:_ [``][function] with contract: callback-last / error-first
Convert sync function to callback-last / error-first contract
### promiseToCallbackLast(promise, callback)
- `promise`: [``][promise]
- `callback`: [``][function]
Convert Promise to callback-last
### promisify(fn)
- `fn`: [``][function] callback-last function
_Returns:_ [``][function] Promise-returning function
Convert async function to Promise-returning function
### promisifySync(fn)
- `fn`: [``][function] regular synchronous function
_Returns:_ [``][function] Promise-returning function
Convert sync function to Promise object
### map(items, fn, done)
- `items`: [``][array] incoming
- `fn`: [``][function] to be executed for each value in the array
- `current`: `` current element being processed in the array
- `callback`: [``][function]
- `err`: [``][error]|[``][null]
- `value`: ``
- `done`: [``][function] on done
- `err`: [``][error]|[``][null]
- `result`: [``][array]
Asynchronous map (iterate parallel)
### filter(items, fn, done)
- `items`: [``][array] incoming
- `fn`: [``][function] to be executed for each value in the array
- `value`: `` item from items array
- `callback`: [``][function]
- `err`: [``][error]|[``][null]
- `accepted`: [``][boolean]
- `done`: [``][function] on done
- `err`: [``][error]|[``][null]
- `result`: [``][array]
Asynchrous filter (iterate parallel)
_Example:_
```js
metasync.filter(
['data', 'to', 'filter'],
(item, callback) => callback(item.length > 2),
(err, result) => console.dir(result),
);
```
### reduce(items, fn, done\[, initial\])
- `items`: [``][array] incoming
- `fn`: [``][function] to be executed for each value in array
- `previous`: `` value previously returned in the last iteration
- `current`: `` current element being processed in the array
- `callback`: [``][function] callback for returning value back to
reduce function
- `err`: [``][error]|[``][null]
- `data`: `` resulting value
- `counter`: [``][number] index of the current element being processed
in array
- `items`: [``][array] the array reduce was called upon
- `done`: [``][function] on done
- `err`: [``][error]|[``][null]
- `result`: [``][array]
- `initial`: `` optional value to be used as first argument in first
iteration
Asynchronous reduce
### reduceRight(items, fn, done\[, initial\])
- `items`: [``][array] incoming
- `fn`: [``][function] to be executed for each value in array
- `previous`: `` value previously returned in the last iteration
- `current`: `` current element being processed in the array
- `callback`: [``][function] callback for returning value back to
reduce function
- `err`: [``][error]|[``][null]
- `data`: `` resulting value
- `counter`: [``][number] index of the current element being processed
in array
- `items`: [``][array] the array reduce was called upon
- `done`: [``][function] on done
- `err`: [``][error]|[``][null]
- `result`: [``][array]
- `initial`: `` optional value to be used as first argument in first
iteration
Asynchronous reduceRight
### each(items, fn, done)
- `items`: [``][array] incoming
- `fn`: [``][function]
- `value`: `` item from items array
- `callback`: [``][function]
- `err`: [``][error]|[``][null]
- `done`: [``][function] on done
- `err`: [``][error]|[``][null]
- `items`: [``][array]
Asynchronous each (iterate in parallel)
_Example:_
```js
metasync.each(
['a', 'b', 'c'],
(item, callback) => {
console.dir({ each: item });
callback();
},
(err, data) => console.dir('each done'),
);
```
### series(items, fn, done)
- `items`: [``][array] incoming
- `fn`: [``][function]
- `value`: `` item from items array
- `callback`: [``][function]
- `err`: [``][error]|[``][null]
- `done`: [``][function] on done
- `err`: [``][error]|[``][null]
- `items`: [``][array]
Asynchronous series
_Example:_
```js
metasync.series(
['a', 'b', 'c'],
(item, callback) => {
console.dir({ series: item });
callback();
},
(err, data) => {
console.dir('series done');
},
);
```
### find(items, fn, done)
- `items`: [``][array] incoming
- `fn`: [``][function]
- `value`: `` item from items array
- `callback`: [``][function]
- `err`: [``][error]|[``][null]
- `accepted`: [``][boolean]
- `done`: [``][function] on done
- `err`: [``][error]|[``][null]
- `result`: ``
Asynchronous find (iterate in series)
_Example:_
```js
metasync.find(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
(item, callback) => callback(null, item % 3 === 0 && item % 5 === 0),
(err, result) => {
console.dir(result);
},
);
```
### every(items, fn, done)
- `items`: [``][array] incoming
- `fn`: [``][function]
- `value`: `` item from items array
- `callback`: [``][function]
- `err`: [``][error]|[``][null]
- `accepted`: [``][boolean]
- `done`: [``][function] on done
- `err`: [``][error]|[``][null]
- `result`: [``][boolean]
Asynchronous every
### some(items, fn, done)
- `items`: [``][array] incoming
- `fn`: [``][function]
- `value`: `` item from items array
- `callback`: [``][function]
- `err`: [``][error]|[``][null]
- `accepted`: [``][boolean]
- `done`: [``][function] on done
- `err`: [``][error]|[``][null]
- `result`: [``][boolean]
Asynchronous some (iterate in series)
### asyncMap(items, fn\[, options\]\[, done\])
- `items`: [``][array] incoming dataset
- `fn`: [``][function]
- `item`: ``
- `index`: [``][number]
- `options`: [`
近期下载者:
相关文件:
收藏者: