promise4solo

1.0.3 • Public • Published

promise4solo

Build Status Test Coverage Code Climate Issue Count

Installation

$ npm install --save promise4solo

Example

var solo = require('promise4solo')();
var now = function () {
  this.start = this.start || Date.now();
  return Date.now() - this.start;
};
 
var singAsync = function (ms) {
  var self = this;
  var ts = now();
  if (!ms) {
    throw new Error(ts + self.name + ' oops');
  }
  console.log(ts + self.name + ' begin sing ' + ms);
  return new Promise(function (resolve) {
    setTimeout(function () {
      var end = now();
      console.log(end + self.name + ' end sing ' + ms);
      resolve(end);
    }, ms);
  });
};
 
var who = {
  name: ' joey'
};
 
var singSolo = solo(singAsync, who);
var saySolo = solo(function (msg) {
  console.log(now() + this.name + ' say ' + msg);
}, who);
 
singSolo(1000);
saySolo('hi');
singSolo().catch(function (err) {
  console.log(err.message);
});
singSolo(100).then(function (end) {
  console.log(end + ' done');
});
console.log(now() + ' start');
 
// Output:
// 0 start
// 3 joey begin sing 1000
// 1011 joey end sing 1000
// 1011 joey say hi
// 1011 joey oops
// 1012 joey begin sing 100
// 1118 joey end sing 100
// 1118 done

API

Return a solo wrap function, and the wrap function will return promise and guarantee one async function be run for order async flow.

solo(func[, thisArg])

var funcAsync = function(val){
  return new Promise(function(resolve){
    //...
    resolve(val);
  });
}
 
var funcSolo = solo(funcAsync);
funcSolo(true).then(function (val) {
 
});

Package Sidebar

Install

npm i promise4solo

Weekly Downloads

3

Version

1.0.3

License

ISC

Last publish

Collaborators

  • josudoey