$ npm install metasync
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
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]]
composed([data], callback)
- async composed, callback-last and err-first contractscomposed.timeout(msec)
- set timeoutcomposed.cancel()
- calcel execution where possiblecomposed.clone()
- clone composedcomposed.pause()
- pause executioncomposed.resume()
- resume execution
metasync.collect(expected)(key, error, value)
- expected - count or array of string
- Returns: collector instance
collector.collect(key, error, value)
- pick or failcollector.pick(key, value)
- pick a keycollector.fail(key, error)
- fail a keycollector.take(key, method, ...arguments)
- take method resultcollector.timeout(msec)
- set timeoutcollector.done(callback)
- set done listener with err-first contractcollector.distinct(true/false)
- deny unlisted keys
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);
metasync.parallel(fns, data, callback)
fns
- array of callback-last functions, callback contranct err-firstdata
- incoming data (optional)callback
- err-first function on done
Example:
metasync.parallel([f1, f2, f3], (err, data) => {});
metasync.sequential(fns, data, callback)
fns
- array of callback-last functions, callback contranct err-firstdata
- incoming data (optional)callback
- err-first function on done
Example:
metasync.sequential([f1, f2, f3], (err, data) => {});
metasync.firstOf(fns, callback)
fns
- array of callback-last functions, callback contranct err-firstcallback
- err-first function on done
metasync.map(items, fn, done)
items
- incoming arrayfn
- callback-last(current, callback) => callback(err, value)
- to be executed for each value in the array
current
- current element being processed in the arraycallback
- err-first
done
- optional err-first callback
metasync.filter(items)
items
- incoming array
Example:
metasync.filter(
['data', 'to', 'filter'],
(item, callback) => callback(item.length > 2),
(err, result) => console.dir(result)
);
metasync.reduce(items, callback, done, initial)
items
- incoming arraycallback
- function to be executed for each value in arrayprevious
- value previously returned in the last iterationcurrent
- current element being processed in the arraycallback
- callback for returning value back to function reducecounter
- index of the current element being processed in arrayitems
- the array reduce was called upon
done
- optional on done callbackfunction(err, result)
initial
- optional value to be used as first arpument in first iteration
metasync.each(items, fn, done)
items
- incoming arrayfn
- callback-last(value, callback) => callback(err)
value
- item from items arraycallback
- callbackfunction(err)
done
- optional on done callbackfunction(err)
Example:
metasync.each(
['a', 'b', 'c'],
(item, callback) => {
console.dir({ each: item });
callback();
},
(err, data) => console.dir('each done')
);
metasync.series(items, fn, done)
items
- incoming arrayfn
- callback-last(value, callback) => callback(err)
value
- item from items arraycallback
- callback(err)
done
optional on done callbackfunction(err)
Example:
metasync.series(
['a', 'b', 'c'],
(item, callback) => {
console.dir({ series: item });
callback();
},
(err, data) => {
console.dir('series done');
}
);
metasync.find(items, fn, done)
items
- incoming arrayfn
- callback-last(value, callback) => callback(err, accepted)
value
- item from items arraycallback
- callback function(err, accepted)
done
- optional on done callbackfunction(err, result)
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);
}
);
metasync.every(items, fn, done)
items
- incoming arrayfn
- callback-last(value, callback) => callback(err, fits)
value
- item from items arraycallback
- callback function(err, fits)
done
- optional on done callbackfunction(err, result)
metasync.some(items)
items
- incoming array
metasync.for(array)
array
- start mutations from this data
new metasync.ConcurrentQueue(concurrency, timeout)
concurrency
- number of simultaneous and asynchronously executing taskstimeout
- process timeout (optional), for single item
metasync.throttle(timeout, fn, ...args)
timeout
- msec intervalfn
- function to be throttled...args
- arguments for fn (optional)
metasync.debounce(timeout, fn, ...args)
timeout
- msecfn
- function to be debounced...args
- arguments for fn (optional)
metasync.timeout(timeout, fn, callback)
timeout
- time intervalfn
- async function to be executedcallback
- callback function on done
metasync.queue(concurrency)
concurrency
- number of simultaneous and asynchronously executing tasks
to function with args as separate values and callback
metasync.toAsync(fn)
fn
- function contract callback-last, callback contranct err-first- Returns: function with arguments gathered from args as separate values and callback
metasync.asAsync(fn, ...args)
fn
- function...args
- its arguments
metasync.callbackify(source)
source
- promise or regular synchronous function- Returns: callback, function
metasync.promisify(func)
func:function
- callback-last function- Returns: object, Promise instance
metasync.promisifySync(func)
func:function
- regular synchronous function- Returns: object, Promise instance
- Timur Shemsedinov (marcusaurelius)
- See github for full contributors list