Comparing version 2.0.0 to 2.1.0
31
index.js
'use strict'; | ||
var once = require('once'); | ||
var isPromise = require('is-promise'); | ||
@@ -22,11 +21,3 @@ var promiseResolver = require('promise-resolver'); | ||
var async = false; | ||
var promise = null; | ||
cb = cb && once(cb); | ||
if (typeof Promise !== 'undefined') { | ||
promise = new Promise(function (resolve, reject) { | ||
cb = promiseResolver(resolve, reject, cb); | ||
}); | ||
} else if (!cb) { | ||
throw new Error('No Native Promise Implementation: You must use a callback function or upgrade to Node >= 0.11.13'); | ||
} | ||
var deferred = promiseResolver.defer(cb); | ||
@@ -37,3 +28,3 @@ try { | ||
async = true; | ||
return cb; | ||
return deferred.cb; | ||
} | ||
@@ -44,13 +35,23 @@ }, Array.prototype.slice.call(arguments)); | ||
if (isPromise(answer)) { | ||
answer.then(cb.bind(null, null), cb); | ||
answer.then(deferred.resolve, deferred.reject); | ||
} else { | ||
setImmediate(cb.bind(null, null, answer)); | ||
deferred.cb(null, answer); | ||
} | ||
} | ||
} catch (e) { | ||
setImmediate(cb.bind(null, e)); | ||
deferred.cb(e); | ||
} | ||
return promise; | ||
return deferred.promise; | ||
} | ||
}; | ||
module.exports.cb = function (func, cb) { | ||
return module.exports(function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
if (args.length === func.length - 1) { | ||
args.push(this.async()); | ||
} | ||
return func.apply(this, args); | ||
}, cb); | ||
}; |
{ | ||
"name": "run-async", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Utility method to run function either synchronously or asynchronously using the common `this.async()` style.", | ||
@@ -26,9 +26,8 @@ "main": "index.js", | ||
"is-promise": "^2.1.0", | ||
"once": "^1.3.0", | ||
"promise-resolver": "^1.0.0" | ||
"promise-resolver": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"bluebird": "^2.10.2", | ||
"mocha": "^1.21.4" | ||
"mocha": "^2.3.3", | ||
"pinkie": "^1.0.0" | ||
} | ||
} |
@@ -55,5 +55,20 @@ Run Async | ||
return 'done running sync function'; | ||
}; | ||
}); | ||
``` | ||
### runAsync.cb | ||
`runAsync.cb` supports all the function types that `runAsync` does and additionally a traditional **callback as the last argument** signature: | ||
```js | ||
var runAsync = require('run-async'); | ||
// IMPORTANT: The wrapped function must have a fixed number of parameters. | ||
runAsync.cb(function(a, b, cb) { | ||
cb(null, a + b); | ||
}, function(err, result) { | ||
console.log(result) | ||
})(1, 2) | ||
``` | ||
If your version of node support Promises natively (node >= 0.12), `runAsync` will return a promise. Example: `runAsync(func)(arg1, arg2).then(cb)` | ||
@@ -60,0 +75,0 @@ |
46
test.js
@@ -10,3 +10,3 @@ 'use strict'; | ||
describe('runAsync', function () { | ||
var Promise = require('bluebird'); | ||
var Promise = require('pinkie'); | ||
@@ -138,4 +138,46 @@ it('run synchronous method', function (done) { | ||
runAsync(returns)(); | ||
}, /No Native Promise Implementation/); | ||
}, /No Promise Implementation/); | ||
}); | ||
}); | ||
describe('runAsync.cb', function () { | ||
it('handles callback parameter', function (done) { | ||
var fn = function (cb) { | ||
setImmediate(function () { | ||
cb(null, 'value'); | ||
}); | ||
}; | ||
runAsync.cb(fn, function (err, val) { | ||
assert.ifError(err); | ||
assert.equal('value', val); | ||
done(); | ||
})(); | ||
}); | ||
it('run synchronous method', function (done) { | ||
var ranAsync = false; | ||
var aFunc = function () { | ||
return 'pass1'; | ||
}; | ||
runAsync.cb(aFunc, function (err, val) { | ||
assert.ifError(err); | ||
assert(ranAsync); | ||
assert.equal(val, 'pass1'); | ||
done(); | ||
})(); | ||
ranAsync = true; | ||
}); | ||
ifPromise('handles a returned promise', function (done) { | ||
var aFunc = function (a) { | ||
return Promise.resolve('foo' + a); | ||
}; | ||
runAsync.cb(aFunc, function(err, result) { | ||
assert.ifError(err); | ||
assert.equal(result, 'foobar'); | ||
done(); | ||
})('bar'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10260
2
203
80
+ Addedasap@2.0.6(transitive)
+ Addeddezalgo@1.0.4(transitive)
+ Addednative-or-bluebird@1.2.0(transitive)
+ Addedpromise-resolver@3.0.0(transitive)
+ Addedsafecb@0.0.1(transitive)
- Removedonce@^1.3.0
- Removedpromise-resolver@1.1.0(transitive)
Updatedpromise-resolver@^3.0.0