1.1.1 • Published 8 years ago

cb2p v1.1.1

Weekly downloads
12
License
MIT
Repository
github
Last release
8 years ago

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.