Comparing version 1.0.0 to 2.0.0
55
index.js
@@ -5,33 +5,50 @@ 'use strict'; | ||
var isPromise = require('is-promise'); | ||
var promiseResolver = require('promise-resolver'); | ||
/** | ||
* Run a function asynchronously or synchronously | ||
* Return a function that will run a function asynchronously or synchronously | ||
* | ||
* example: | ||
* runAsync(wrappedFunction, callback)(...args); | ||
* | ||
* @param {Function} func Function to run | ||
* @param {Function} cb Callback function passed the `func` returned value | ||
* @...rest {Mixed} rest Arguments to pass to `func` | ||
* @return {Null} | ||
* @return {Function(arguments)} Arguments to pass to `func`. This function will in turn | ||
* return a Promise (Node >= 0.12) or call the callbacks. | ||
*/ | ||
module.exports = function (func, cb) { | ||
var async = false; | ||
cb = once(cb); | ||
return function () { | ||
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'); | ||
} | ||
try { | ||
var answer = func.apply({ | ||
async: function () { | ||
async = true; | ||
return cb; | ||
} | ||
}, Array.prototype.slice.call(arguments, 2) ); | ||
try { | ||
var answer = func.apply({ | ||
async: function () { | ||
async = true; | ||
return cb; | ||
} | ||
}, Array.prototype.slice.call(arguments)); | ||
if (!async) { | ||
if (isPromise(answer)) { | ||
answer.then(cb.bind(null, null), cb); | ||
} else { | ||
setImmediate(cb.bind(null, null, answer)); | ||
if (!async) { | ||
if (isPromise(answer)) { | ||
answer.then(cb.bind(null, null), cb); | ||
} else { | ||
setImmediate(cb.bind(null, null, answer)); | ||
} | ||
} | ||
} catch (e) { | ||
setImmediate(cb.bind(null, e)); | ||
} | ||
} catch (e) { | ||
setImmediate(cb.bind(null, e)); | ||
return promise; | ||
} | ||
}; |
{ | ||
"name": "run-async", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Utility method to run function either synchronously or asynchronously using the common `this.async()` style.", | ||
@@ -26,3 +26,4 @@ "main": "index.js", | ||
"is-promise": "^2.1.0", | ||
"once": "^1.3.0" | ||
"once": "^1.3.0", | ||
"promise-resolver": "^1.0.0" | ||
}, | ||
@@ -29,0 +30,0 @@ "devDependencies": { |
@@ -6,6 +6,4 @@ Run Async | ||
Utility method to run function either synchronously or asynchronously using the common `this.async()` style. Also provides for promise returning functions. | ||
Utility method to run a function either synchronously or asynchronously using a series of common patterns. This is useful for library author accepting sync or async functions as parameter. `runAsync` will always run them as an async method, and normalize the multiple signature. | ||
This is useful for library author accepting sync or async functions as parameter. `runAsync` will always run them as async method, and normalize the function handling. | ||
Installation | ||
@@ -21,38 +19,44 @@ ========= | ||
Here's a simple example print the function results and three options a user can provide a function. | ||
```js | ||
var runAsync = require('run-async'); | ||
// In Async mode: | ||
var asyncFn = function (a) { | ||
var printAfter = function (func) { | ||
var cb = function (err, returnValue) { | ||
console.log(returnValue); | ||
}; | ||
runAsync(func, cb)(/* arguments for func */); | ||
}; | ||
``` | ||
#### Using `this.async` | ||
```js | ||
printAfter(function () { | ||
var done = this.async(); | ||
setTimeout(function () { | ||
done(null, 'running: ' + a); | ||
done(null, 'done running with callback'); | ||
}, 10); | ||
}; | ||
}); | ||
``` | ||
runAsync(asyncFn, function (err, answer) { | ||
console.log(answer); // 'running: async' | ||
}, 'async'); | ||
// In Sync mode: | ||
var syncFn = function (a) { | ||
return 'running: ' + a; | ||
}; | ||
runAsync(syncFn, function (err, answer) { | ||
console.log(answer); // 'running: sync' | ||
}, 'sync'); | ||
var promiseFunc = function(a) { | ||
#### Returning a promise | ||
```js | ||
printAfter(function () { | ||
return new Promise(function (resolve, reject) { | ||
resolve('running: ' + a); | ||
resolve('done running with promises'); | ||
}); | ||
} | ||
}); | ||
``` | ||
runAsync(promiseFunc, function (err, answer) { | ||
console.log(answer); // 'running: promise' | ||
}, 'promise'); | ||
#### Synchronous function | ||
```js | ||
printAfter(function () { | ||
return 'done running sync function'; | ||
}; | ||
``` | ||
If your version of node support Promises natively (node >= 0.12), `runAsync` will return a promise. Example: `runAsync(func)(arg1, arg2).then(cb)` | ||
Licence | ||
@@ -59,0 +63,0 @@ ======== |
50
test.js
'use strict'; | ||
var hasGlobalPromise = typeof Promise !== 'undefined'; | ||
var ifPromise = hasGlobalPromise ? it : it.skip; | ||
var notPromise = hasGlobalPromise ? it.skip : it; | ||
var assert = require('assert'); | ||
var runAsync = require('./index'); | ||
var Promise = require('bluebird'); | ||
describe('runAsync', function () { | ||
var Promise = require('bluebird'); | ||
it('run synchronous method', function (done) { | ||
@@ -18,3 +22,3 @@ var ranAsync = false; | ||
done(); | ||
}); | ||
})(); | ||
ranAsync = true; | ||
@@ -33,3 +37,3 @@ }); | ||
done(); | ||
}); | ||
})(); | ||
}); | ||
@@ -46,3 +50,3 @@ | ||
done(); | ||
}, 1, 'bar'); | ||
})(1, 'bar'); | ||
}); | ||
@@ -60,3 +64,3 @@ | ||
done(); | ||
}); | ||
})(); | ||
}); | ||
@@ -77,3 +81,3 @@ | ||
done(); | ||
}); | ||
})(); | ||
}); | ||
@@ -90,3 +94,3 @@ | ||
done(); | ||
}); | ||
})(); | ||
}); | ||
@@ -107,4 +111,36 @@ | ||
done(); | ||
})(); | ||
}); | ||
ifPromise('returns a promise that is resolved', function (done) { | ||
var returns = function () { | ||
return 'hello'; | ||
}; | ||
runAsync(returns)().then(function (result) { | ||
assert.equal(result, 'hello'); | ||
done(); | ||
}); | ||
}); | ||
ifPromise('returns a promise that is rejected', function (done) { | ||
var throws = function () { | ||
throw new Error('sync error'); | ||
}; | ||
runAsync(throws)().catch(function (reason) { | ||
assert.equal(reason.message, 'sync error'); | ||
done(); | ||
}); | ||
}); | ||
notPromise('throws a helpful error message if no cb, and no global.Promise', function () { | ||
var returns = function () { | ||
return 'hello'; | ||
}; | ||
assert.throws(function () { | ||
runAsync(returns)(); | ||
}, /No Native Promise Implementation/); | ||
}); | ||
}); |
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
9042
166
65
3
+ Addedpromise-resolver@^1.0.0
+ Addedpromise-resolver@1.1.0(transitive)