Skip to content

post-Industrial/MetaSync

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Meta Async Programming Approach

Build Status Dependency Status

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, function(data) {
  console.dir(Object.keys(data));
});

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

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

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

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

Parallel execution

metasync.parallel([f1, f2, f3], function done() {});

Sequential execution

metasync.sequential([f1, f2, f3], function done() {});

Asynchrous filter

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

Asynchrous find

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

Asyncronous series (sequential)

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

Asyncronous each (parallel)

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

Contributors

About

Meta Async Programming Approach

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%