node-callback-to-promise
v2.0.1
Published
Callback to promise
Downloads
3
Readme
node-callback-to-promise
Introduction
Encapsulate a method with a node-style callback in a Promise.
API
//
// encapsulate a method with a node-style callback in a Promise
//
// @param {function} fn function to be encapsulated
// @param {object} context 'this' of the encapsulated function
// @param {Array<any>} args to be passed to the called function
//
// @return {Promise} a Promise encapsulating the function
//
function promise(fn, context, args)
Install
npm install node-callback-to-promise
Usage
Suppose you provide an API with callback,
but you also want to return Promise if caller not pass callback.
callback-to-promise
will help you in this scenario. Here is an example:
const callbackToPromise = require('node-callback-to-promise');
userSchema.methods.authenticate = function authenticate(plainTextPassword, callback) {
if (!callback) {
// if no callback, return a promise
return callbackToPromise.promise(authenticate, this, [plainTextPassword]);
}
...
}