1.0.1 • Published 7 years ago

promisify-optional-callback v1.0.1

Weekly downloads
87
License
MIT
Repository
github
Last release
7 years ago

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.