🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

callbackify

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

callbackify - npm Package Compare versions

Comparing version

to
1.1.0

19

index.js

@@ -20,2 +20,21 @@ function callbackify(fn) {

function callbackifyVariadic(fn) {
return function () {
var args = [].slice.call(arguments)
var ctx = this
if (args.length >= 1 &&
typeof args[args.length - 1] === 'function') {
// callback mode
var cb = args.pop()
fn.apply(this, args)
.then(function (val) { cb.call(ctx, null, val) },
function (err) { cb.call(ctx, err) })
return
}
// promise mode
return fn.apply(ctx, arguments)
}
}
module.exports = callbackify
module.exports.variadic = callbackifyVariadic;

2

package.json
{
"name": "callbackify",
"author": "jden <jason@denizac.org>",
"version": "1.0.0",
"version": "1.1.0",
"description": "backwards compatibilify your callback functions while migrating apis to promises",

@@ -6,0 +6,0 @@ "keywords": [

@@ -35,4 +35,25 @@ # callbackify

Note, `callbackify` only works on fixed-parameter length functions, not variadic functions. It determines whether or not you're passing in a continuation callback by counting parameters. If you can think of a more clever way, please send a PR!
Normally, callbackify will only work with fixed-parameter-length functions, and will use the declared
parameter length to determine if the extra callback argument is present. If you need to use
callbackify with variadic functions, or functions that don't declare their full argument
list, you can use:
```js
// options argument is optional
var getUserById = callbackify.variadic(function (id, options) {
if (options === undefined) { options = {} }
if (options.select) {
return db.users.byId(id).select(options.select).first()
} else {
return db.users.byId(id).first()
}
})
// we can do either of these
getUserById(23, function (err, user) { })
getUserById(23, { select: [ 'name' ] }, function (err, user) {} )
```
Note that this will not work if the last argument your function can take
is a function, as that last argument will always be detected as a callback
function.
## api

@@ -39,0 +60,0 @@

@@ -83,2 +83,14 @@ var chai = require('chai')

it('detects the callback with variadic functions', function (done) {
var foo = callbackify.variadic(function () {
return Promise.resolve(arguments[0])
})
foo(44, function (err, val) {
val.should.equal(44)
done()
})
})
})