$ npm install metasync
metasync(fns)(data, done)
fns
- array of callback-last functions, callback contranct err-firstdata
- input data (optional)done
- err-first callback- Returns: composed callback-last / err-first function
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:
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 function
Returns: <Function>
Convert Promise-returning to callback-last / error-first contract
fn
:<Function>
regular synchronous function
Returns: <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 function
Returns: <Function>
Promise-returning function
Convert async function to Promise-returning function
fn
:<Function>
regular synchronous function
Returns: <Function>
Promise-returning function
Convert sync function to Promise object
items
:<Array>
incomingfn
:<Function>
to be executed for each value in the arraycurrent
:<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 arrayvalue
:<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 arrayprevious
:<any>
value previously returned in the last iterationcurrent
:<any>
current element being processed in the arraycallback
:<Function>
callback for returning value back to reduce functioncounter
:<number>
index of the current element being processed in arrayitems
:<Array>
the array reduce was called upon
done
:<Function>
on doneinitial
:<any>
optional value to be used as first argument in first iteration
Asynchronous reduce
items
:<Array>
incomingfn
:<Function>
to be executed for each value in arrayprevious
:<any>
value previously returned in the last iterationcurrent
:<any>
current element being processed in the arraycallback
:<Function>
callback for returning value back to reduce functioncounter
:<number>
index of the current element being processed in arrayitems
:<Array>
the array reduce was called upon
done
:<Function>
on doneinitial
:<any>
optional value to be used as first argument in first iteration
Asynchronous 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, optionaldone
:<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 keys
Data 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 fn
Returns: <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-first
Returns: <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-first
Executes all asynchronous functions and pass first result to callback
fns
:<Function[]>
callback-last / err-firstcontext
:<Object>
incoming data, optionalcallback
:<Function>
on done, err-first
Parallel 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 done
Sequential execution
Example:
metasync.sequential([f1, f2, f3], (err, data) => {});
condition
:<any>
defaultVal
:<any>
optional, value that will be returned to callback ifcondition
is falsy.asyncFn
:<Function>
callback-last function that will be executed ifcondition
if truthyargs
:<any[]>
args to pass toasyncFn
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 toasyncFn
Run asyncFn
if it is provided
fn
:<Function>
callback-last / err-first
Returns: <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 arguments
Wrap 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 async
Returns: <Function>
memoized
Create memoized function
eventName
:<string>
listener
:<Function>
handler
Add 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 arguments
Emit Memoized events
concurrency
:<number>
simultaneous and asynchronously executing tasks
Returns: <Queue>
Create Queue instance
Queue constructor
concurrency
:<number>
asynchronous concurrency
Queue constructor
msec
:<number>
wait timeout for single item
Returns: <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>
optional
Returns: <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 successitem
:<any>
Returns: <this>
Set listener on processing success
listener
:<Function>
on failure
Returns: <this>
Set listener on processing error
listener
:<Function>
on drain
Returns: <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 mode
Returns: <this>
Activate or deactivate priority mode
flag
:<boolean>
default: true, false will disable roundRobin mode
Returns: <this>
Activate or deactivate round robin mode
dest
:<Queue>
destination queue
Returns: <this>
Pipe processed items to different queue
timeout
:<number>
msec intervalfn
:<Function>
to be throttledargs
:<Array>
arguments for fn, optional
Returns: <Function>
Get throttling function, executed once per interval
timeout
:<number>
msecfn
:<Function>
to be debouncedargs
:<Array>
arguments for fn, optional
Debounce function, delayed execution
timeout
:<number>
time intervalfn
:<Function>
to be executedcallback
:<Function>
callback(...args), on doneargs
:<Array>
Set timeout for asynchronous function execution
- Timur Shemsedinov (marcusaurelius)
- See github for full contributors list