Skip to content

stagas/promisify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promisify

converts a callback based api to promise based

example

var promisify = require('promisify');

var add = promisify(function(a, b, cb){
  setTimeout(function(){
    cb(null, a+b);
  }, 0);
});

var result = add(3, 3);
var again = add(result, 5);
var waaat = add(again, result);
add(result, waaat).when(function(err, res){
  console.log(res); // 23
});

var math = {};
math.add = function(a, b, cb){ cb(null, a+b); };
math.sub = function(a, b, cb){ cb(null, a-b); };
math.mul = function(a, b, cb){ cb(null, a*b); };

var p = promisify(math);

p.add(1,p.sub(6,p.mul(2,2))).when(function(err, res){
  console.log(res); // 3
});

License

MIT

About

converts a callback based api to promise based

Resources

Stars

Watchers

Forks

Packages

No packages published