Skip to content

habi4ek/MetaSync

 
 

Repository files navigation

Meta Async Programming Approach

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

Installation

$ npm install metasync

Examples

Functional Asyncronous Composition

Syntax: metasync.composition(functions, [done, [data]]);

Parameters:

  • functions - array of function([data,] callback) where:
    • data - optional incoming data
    • callback - function([data]) where:
      • data - outgoing data
  • done - callback(data) where:
    • data - hash with of functions results
  • data - incoming data
metasync.composition(
  [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]]

Examples of functions:

// with one argiment and no result
function f1(callback) {
  callback();
}

// with one argiment and returning result
function f2(callback) {
  callback('value');
}

// with two argiments and result
function f3(data, callback) {
  // assign result to data random key
  data.keyName = 'value';
  // returning no result
  callback();
}

An Event-driven Asyncronous Data Collector

var metasync = require('metasync');
var fs = require('fs');

var dataCollector = new metasync.DataCollector(4, (data) => {
  console.dir(Object.keys(data));
});

dataCollector.collect('user', { name: 'Marcus Aurelius' });

fs.readFile('HISTORY.md', (err, data) => {
  dataCollector.collect('history', data);
});

fs.readFile('README.md', (err, data) => {
  dataCollector.collect('readme', data);
});

setTimeout(() => {
  dataCollector.collect('timer', { date: new Date() });
}, 1000);

Parallel execution

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

Sequential execution

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

Asynchrous filter

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

Asynchrous find

metasync.find(
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
  (item, callback) => (
    callback(item % 3 === 0 && item % 5 === 0)
  ),
  (result) => {
    console.dir(result);
  }
);

Asyncronous series (sequential)

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

Asyncronous each (parallel)

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

Contributors

About

Meta Async Programming Approach

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%