npm.io
1.0.1 • Published 9 years ago

promisify-optional-callback

Licence
MIT
Version
1.0.1
Deps
0
Vulns
0
Weekly
0

Promisify

Promisify converts a function that takes a callback into a function that takes a callback or returns a promise.

Installation

npm install promisify-optional-callback

Usage

const promisify = require('promisify-optional-callback');

function example(callback) {
	console.log('Hello world');

	callback();
}

const examplePromise = promisify(example);

examplePromise(function (error) {
	console.log('Inside a callback');
});

// OR

examplePromise()
.then(function () {
	console.log('Inside a promise');
})
.catch(function (error) {
	console.log('Experienced an error');
});

If callback is called with a non-null object as its first argument, that is handled as an error and the promise will reject.