Giter Site home page Giter Site logo

metasync's Introduction

Asynchronous Programming Library

TravisCI bitHound Codacy Badge NPM Version NPM Downloads/Month NPM Downloads

Installation

$ npm install metasync

Create a composed function from flow syntax

metasync.flow(fns)(data, done)

  • fns - array of callback-last functions, callback contranct err-first
  • data - input data
  • done - err-first callback
  • Returns: composed callback-last / err-first function

composition

const f = metasync.flow(
  [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]]

Flow methods:

  • flow(data, callback) - composed flow, callback-last and err-first contracts
  • flow.timeout(msec) - set flow timeout
  • flow.cancel() - calcel flow
  • flow.clone() - clone flow
  • flow.pause() - pause flow
  • flow.resume() - resume flow

Collector

metasync.collect(expected)(key, error, value)

  • expected - count or array of string
  • Returns: collector instance

Collector methods:

  • collector.collect(key, error, value) - pick or fail
  • collector.pick(key, value) - pick a key
  • collector.fail(key, error) - fail a key
  • collector.take(key, method, ...arguments) - take method result
  • collector.timeout(msec) - set timeout
  • collector.done(callback) - set done listener with err-first contract
  • collector.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);

Parallel execution

metasync.parallel(fns, data, callback)

  • fns - array of callback-last functions, callback contranct err-first
  • data - incoming data (optional)
  • callback - err-first function on done

Example: metasync.parallel([f1, f2, f3], (err, data) => {});

Sequential execution

metasync.sequential(fns, data, callback)

  • fns - array of callback-last functions, callback contranct err-first
  • data - incoming data (optional)
  • callback - err-first function on done

Example:

metasync.sequential([f1, f2, f3], (err, data) => {});

Executes all asynchronous functions and pass first result to callback

metasync.firstOf(fns, callback)

  • fns - array of callback-last functions, callback contranct err-first
  • callback - err-first function on done

Asynchronous map (iterate parallel)

metasync.map(items, fn, done)

  • items - incoming array
  • fn - callback-last (current, callback) => callback(err, value)
    • to be executed for each value in the array
    • current - current element being processed in the array
    • callback - err-first
  • done - optional err-first callback

Asynchrous filter (iterate parallel)

metasync.filter(items)

  • items - incoming array

Example:

metasync.filter(
  ['data', 'to', 'filter'],
  (item, callback) => callback(item.length > 2),
  (err, result) => console.dir(result)
);

Asynchronous reduce

metasync.reduce(items, callback, done, initial)

  • items - incoming array
  • callback - 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 - callback for returning value back to function reduce
    • counter - index of the current element being processed in array
    • items - the array reduce was called upon
  • done - optional on done callback function(err, result)
  • initial - optional value to be used as first arpument in first iteration

Asynchronous each (iterate in parallel)

metasync.each(items, fn, done)

  • items - incoming array
  • fn - callback-last (value, callback) => callback(err)
    • value - item from items array
    • callback - callback function(err)
  • done - optional on done callback function(err)

Example:

metasync.each(
  ['a', 'b', 'c'],
  (item, callback) => {
    console.dir({ each: item });
    callback();
  },
  (err, data) => console.dir('each done')
);

Asynchronous series

metasync.series(items, fn, done)

  • items - incoming array
  • fn - callback-last (value, callback) => callback(err)
    • value - item from items array
    • callback - callback (err)
  • done optional on done callback function(err)

Example:

metasync.series(
  ['a', 'b', 'c'],
  (item, callback) => {
    console.dir({ series: item });
    callback();
  },
  (err, data) => {
    console.dir('series done');
  }
);

Asynchronous find (iterate in series)

metasync.find(items, fn, done)

  • items - incoming array
  • fn - callback-last (value, callback) => callback(err, accepted)
    • value - item from items array
    • callback - callback function (err, accepted)
  • done - optional on done callback function(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);
  }
);

Asynchronous every

metasync.every(items, fn, done)

  • items - incoming array
  • fn - callback-last (value, callback) => callback(err, fits)
    • value - item from items array
    • callback - callback function (err, fits)
  • done - optional on done callback function(err, result)

Asynchronous some (iterate in series)

metasync.some(items)

  • items - incoming array

Create an ArrayChain instance

metasync.for(array)

  • array - start mutations from this data

ConcurrentQueue

new metasync.ConcurrentQueue(concurrency, timeout)

  • concurrency - number of simultaneous and asynchronously executing tasks
  • timeout - process timeout (optional), for single item

Function throttling, executed once per interval

metasync.throttle(timeout, fn, args)

  • timeout - msec interval
  • fn - function to be throttled
  • args - arguments array for fn (optional)

Debounce function, delayed execution

metasync.debounce(timeout, fn, args)

  • timeout - msec
  • fn - function to be debounced
  • args - arguments array for fn (optional)

Set timeout for asynchronous function execution

metasync.timeout(timeout, fn, callback)

  • timeout - time interval
  • fn - async function to be executed
  • callback - callback function on done

Queue instantiation

metasync.queue(concurrency)

  • concurrency - number of simultaneous and asynchronously executing tasks

Transforms function with args arguments and callback

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

Async function

metasync.asAsync(fn, ...args)

  • fn - function
  • ...args - its argumants

Convert source to callback-last contract

metasync.callbackify(source)

  • source - promise or regular synchronous function
  • Returns: callback, function

Convert async function to Promise object

metasync.promisify(func)

  • func:function - callback-last function
  • Returns: object, Promise instance

Convert sync function to Promise object

metasync.promisifySync(func)

  • func:function - regular synchronous function
  • Returns: object, Promise instance

Contributors

metasync's People

Contributors

tshemsedinov avatar dzyubspirit avatar nechaido avatar anxolerd avatar aqrln avatar nazarponochevnyi avatar primeare avatar arthur-here avatar

Watchers

James Cloos avatar Dmytro Ostapenko avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.