Asynchronous Programming Library for JavaScript & Node.js
$ npm install metasync
metasync(fns)(data, done)
fns
- array of callback-last functions, callback contranct err-firstdata
- input data (optional)done
- err-first callbackconst composed = metasync([f1, f2, f3, [[f4, f5, [f6, f7], f8]], f9]);
[f1, f2, f3]
[[f1, f2, f3]]
Example:
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);
fn
: <Function>
promise-returning functionReturns: <Function>
Convert Promise-returning to callback-last / error-first contract
fn
: <Function>
regular synchronous functionReturns: <Function>
with contract: callback-last / error-first
Convert sync function to callback-last / error-first contract
promise
: <Promise>
callback
: <Function>
Convert Promise to callback-last
fn
: <Function>
callback-last functionReturns: <Function>
Promise-returning function
Convert async function to Promise-returning function
fn
: <Function>
regular synchronous functionReturns: <Function>
Promise-returning function
Convert sync function to Promise object
items
: <Array>
incomingfn
: <Function>
to be executed for each value in the array
current
: <any>
current element being processed in the arraycallback
: <Function>
done
: <Function>
on done
Asynchronous map (iterate parallel)
items
: <Array>
incomingfn
: <Function>
to be executed for each value in the array
value
: <any>
item from items arraycallback
: <Function>
done
: <Function>
on done
Asynchrous filter (iterate parallel)
Example:
metasync.filter(
['data', 'to', 'filter'],
(item, callback) => callback(item.length > 2),
(err, result) => console.dir(result),
);
items
: <Array>
incomingfn
: <Function>
to be executed for each value in array
previous
: <any>
value previously returned in the last iterationcurrent
: <any>
current element being processed in the arraycallback
: <Function>
callback for returning value back to
reduce function
counter
: <number>
index of the current element being processed
in arrayitems
: <Array>
the array reduce was called upondone
: <Function>
on done
initial
: <any>
optional value to be used as first argument in first
iterationAsynchronous reduce
items
: <Array>
incomingfn
: <Function>
to be executed for each value in array
previous
: <any>
value previously returned in the last iterationcurrent
: <any>
current element being processed in the arraycallback
: <Function>
callback for returning value back to
reduce function
counter
: <number>
index of the current element being processed
in arrayitems
: <Array>
the array reduce was called upondone
: <Function>
on done
initial
: <any>
optional value to be used as first argument in first
iterationAsynchronous reduceRight
items
: <Array>
incomingfn
: <Function>
value
: <any>
item from items arraycallback
: <Function>
done
: <Function>
on done
Asynchronous each (iterate in parallel)
Example:
metasync.each(
['a', 'b', 'c'],
(item, callback) => {
console.dir({ each: item });
callback();
},
(err, data) => console.dir('each done'),
);
items
: <Array>
incomingfn
: <Function>
value
: <any>
item from items arraycallback
: <Function>
done
: <Function>
on done
Asynchronous series
Example:
metasync.series(
['a', 'b', 'c'],
(item, callback) => {
console.dir({ series: item });
callback();
},
(err, data) => {
console.dir('series done');
},
);
items
: <Array>
incomingfn
: <Function>
value
: <any>
item from items arraycallback
: <Function>
done
: <Function>
on done
Asynchronous find (iterate in series)
Example:
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);
},
);
items
: <Array>
incomingfn
: <Function>
value
: <any>
item from items arraycallback
: <Function>
done
: <Function>
on done
Asynchronous every
items
: <Array>
incomingfn
: <Function>
value
: <any>
item from items arraycallback
: <Function>
done
: <Function>
on done
Asynchronous some (iterate in series)
items
: <Array>
incoming datasetfn
: <Function>
item
: <any>
index
: <number>
options
: <Object>
map params, optional
done
: <Function>
call on done, optional
Non-blocking synchronous map
base
: <Iterable>
|<AsyncIterable>
an
iterable that is wrapped in <AsyncIterator>
Returns: <AsyncIterator>
Create an AsyncIterator instance
expected
: <number>
|<string[]>
Returns: <Collector>
Create Collector instance
Data collector
expected
: <number>
|<string[]>
count or keysData collector
Returns: <this>
Pick or fail key
key
: <string>
value
: <any>
Returns: <this>
Pick key
Returns: <this>
Fail key
key
: <string>
fn
: <Function>
args
: <Array>
rest arguments, to be passed in fnReturns: <this>
Take method result
msec
: <number>
Returns: <this>
Set timeout
callback
: <Function>
err
: <Error>
data
: <any>
Returns: <this>
Set on done listener
value
: <boolean>
Returns: <this>
Deny or allow unlisted keys
flow
: <Function[]>
callback-last / err-firstReturns: <Function>
composed callback-last / err-first
Asynchronous functions composition
Array of functions results in sequential execution: [f1, f2, f3]
Double
brackets array of functions results in parallel execution: [[f1, f2, f3]]
Example:
const composed = metasync([f1, f2, f3, [[f4, f5, [f6, f7], f8]], f9]);
Clone composed
Pause execution
Resume execution
msec
: <number>
Set timeout
Cancel execution where possible
fns
: <Function[]>
callback-last / err-firstcallback
: <Function>
on done, err-firstExecutes all asynchronous functions and pass first result to callback
fns
: <Function[]>
callback-last / err-firstcontext
: <Object>
incoming data, optionalcallback
: <Function>
on done, err-firstParallel execution
Example:
metasync.parallel([f1, f2, f3], (err, data) => {});
fns
: <Function[]>
callback-last with err-first contractcontext
: <Object>
incoming data, optionalcallback
: <Function>
err-first on doneSequential execution
Example:
metasync.sequential([f1, f2, f3], (err, data) => {});
condition
: <any>
defaultVal
: <any>
optional, value that will be returned to callback if
condition
is falsy.asyncFn
: <Function>
callback-last function that will be
executed if condition
if truthyargs
: <any[]>
args to pass to asyncFn
Run asyncFn
if condition
is truthy, else return defaultVal
to callback.
asyncFn
: <Function>
callback-last function that will be
executed if it is providedargs
: <any[]>
args to pass to asyncFn
Run asyncFn
if it is provided
fn
: <Function>
callback-last / err-firstReturns: <Function>
Convert synchronous function to asynchronous
Transform function with args arguments and callback to function with args as separate values and callback
fn
: <Function>
asynchronousargs
: <Array>
its argumentsWrap function adding async chain methods
args
: <Array>
Applicative f => a -> f a
fn1
: <Function>
fn2
: <Function>
Monoid m => a -> a -> a
fn1
: <Function>
f
: <Function>
Functor f => (a -> b) -> f a -> f b
fn
: <Function>
funcA
: <Function>
Applicative f => f (a -> b) -> f a -> f b
fn
: <Function>
sync or asyncReturns: <Function>
memoized
Create memoized function
eventName
: <string>
listener
: <Function>
handlerAdd event listener
Example:
const memoized = new Memoized();
memoized.on('memoize', (err, data) => { ... });
memoized.on('add', (key, err, data) => { ... });
memoized.on('del', (key) => { ... })
memoized.on('clear', () => { ... });
eventName
: <string>
args
: <any>
rest argumentsEmit Memoized events
concurrency
: <number>
simultaneous and asynchronously executing
tasksReturns: <Queue>
Create Queue instance
Queue constructor
concurrency
: <number>
asynchronous concurrencyQueue constructor
msec
: <number>
wait timeout for single itemReturns: <this>
Set wait before processing timeout
Returns: <this>
Throttle to limit throughput
item
: <Object>
to be addedfactor
: <number>
|<string>
type, source, destination
or path, optionalpriority
: <number>
optionalReturns: <this>
Add item to queue
task
: <Array>
next task [item, factor, priority]Returns: <this>
Process next item
Returns: <this>
Prepare next item for processing
Returns: <this>
Pause queue
This function is not completely implemented yet
Returns: <this>
Resume queue
This function is not completely implemented yet
Returns: <this>
Clear queue
msec
: <number>
process timeout for single itemonTimeout
: <Function>
Returns: <this>
Set timeout interval and listener
fn
: <Function>
item
: <Object>
callback
: <Function>
Returns: <this>
Set processing function
fn
: <Function>
done listener
Returns: <this>
Set listener on processing done
listener
: <Function>
on success
item
: <any>
Returns: <this>
Set listener on processing success
listener
: <Function>
on failure
Returns: <this>
Set listener on processing error
listener
: <Function>
on drainReturns: <this>
Set listener on drain Queue
Returns: <this>
Switch to FIFO mode (default for Queue)
Returns: <this>
Switch to LIFO mode
flag
: <boolean>
default: true, false will disable priority modeReturns: <this>
Activate or deactivate priority mode
flag
: <boolean>
default: true, false will disable roundRobin
modeReturns: <this>
Activate or deactivate round robin mode
dest
: <Queue>
destination queueReturns: <this>
Pipe processed items to different queue
timeout
: <number>
msec intervalfn
: <Function>
to be throttledargs
: <Array>
arguments for fn, optionalReturns: <Function>
Get throttling function, executed once per interval
timeout
: <number>
msecfn
: <Function>
to be debouncedargs
: <Array>
arguments for fn, optionalDebounce function, delayed execution
timeout
: <number>
time intervalfn
: <Function>
to be executedcallback
: <Function>
callback(...args), on done
args
: <Array>
Set timeout for asynchronous function execution