r3b-promise

0.0.1 • Public • Published

promise

A very minimal Promise implementation. Creates a simple deferred object with utility methods for sequential or parallel execution. that's it.

var Promise= require('r3b-promise');

function asyncTask(options, callback){
  //do something
  callback();
}

function postProcessingTask(err, result){
  //process
}

function process(options){
  var p = new Promise();
  asyncTask(options, function(err, result){
    p.done(err, result);
  }
  return p;
}

function run(){
  process({}).then(postProcessingTask);
}

function runInParallel(){
  Promise.join([
    asyncTask({property:'value1'}),
    asyncTask({property:'value2'}),
    asyncTask({property:'value3'}),
  ]).then(function(errors, results){
    errors.forEach(function(err){
      //handle error
    });
    results.forEach(function(result){
      //handle result
    });
  });
}

function step(err, data){
  var p = new Promise();
  passItOn(function(err, result){
    p.done(err, result);
  }
  return p;
}
function runInSequence(){
  Promise.chain([
    step({property:'value1'}),
    step({property:'value2'}),
    step({property:'value3'}),
  ]);
}

Readme

Keywords

Package Sidebar

Install

npm i r3b-promise

Weekly Downloads

0

Version

0.0.1

License

APLv2

Last publish

Collaborators

  • r3b