Skip to content

Convert node-style callback function to Promise APIs style.

License

Notifications You must be signed in to change notification settings

purplestone/cb2p

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cb2p

Convert node-style callback function to Promise APIs style.

Getting Started

Install the module: npm i cb2p

Examples

var $$cb2p = require('cb2p');

$$cb2p(require('fs').readdir)('./').then(function () {
	console.log(arguments);
}).catch(function () {
	console.log(arguments);
});

$$cb2p(require('fs').readdir).p2cb('./', function () {
	console.log(arguments);
});

call object method:

var oTest = {
	a : 'eg',
	getData : function (id, cb) {
		var that = this;
		var iTimeout = setTimeout(function() {
			cb(0, 'ok ' + that.a + ' ' + id);
		}, 100);
	}
};

$$cb2p(oTest, 'getData')(3).then(function () {
	console.log(arguments);
});

$$cb2p(oTest, oTest.getData)(3).then(function () {
	console.log(arguments);
});

$$cb2p(oTest.getData).call(oTest, 3).then(function () {
	console.log(arguments);
});

$$cb2p(oTest.getData).bind(oTest)(3).then(function () {
	console.log(arguments);
});

License

Licensed under the MIT license.

About

Convert node-style callback function to Promise APIs style.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published