promisify-optional-callback
v1.0.1
Published
Converts a callback function into a callback + promisify function
Downloads
25
Readme
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.