diff options
-rw-r--r-- | intro.md | 3 | ||||
-rw-r--r-- | lib/asyncify.js | 19 | ||||
-rw-r--r-- | lib/internal/wrapAsync.js | 16 | ||||
-rw-r--r-- | mocha_test/asyncFunctions.js | 13 | ||||
-rw-r--r-- | mocha_test/asyncify.js | 38 |
5 files changed, 68 insertions, 21 deletions
@@ -14,9 +14,8 @@ it can also be used directly in the browser. Async is also installable via: +- [yarn](https://yarnpkg.com/en/): `yarn add async` - [bower](http://bower.io/): `bower install async` -- [component](https://github.com/componentjs/component): `component install caolan/async` -- [jam](http://jamjs.org/): `jam install async` Async provides around 70 functions that include the usual 'functional' suspects (`map`, `reduce`, `filter`, `each`…) as well as some common patterns diff --git a/lib/asyncify.js b/lib/asyncify.js index 96eea54..d5d0bee 100644 --- a/lib/asyncify.js +++ b/lib/asyncify.js @@ -1,5 +1,6 @@ import isObject from 'lodash/isObject'; import initialParams from './internal/initialParams'; +import setImmediate from './internal/setImmediate'; /** * Take a sync function and make it async, passing its return value to a @@ -20,7 +21,7 @@ import initialParams from './internal/initialParams'; * @method * @alias wrapSync * @category Util - * @param {Function} func - The synchronous funuction, or Promise-returning + * @param {Function} func - The synchronous function, or Promise-returning * function to convert to an {@link AsyncFunction}. * @returns {AsyncFunction} An asynchronous wrapper of the `func`. To be * invoked with `(args..., callback)`. @@ -68,12 +69,24 @@ export default function asyncify(func) { // if result is Promise object if (isObject(result) && typeof result.then === 'function') { result.then(function(value) { - callback(null, value); + invokeCallback(callback, null, value); }, function(err) { - callback(err.message ? err : new Error(err)); + invokeCallback(callback, err.message ? err : new Error(err)); }); } else { callback(null, result); } }); } + +function invokeCallback(callback, error, value) { + try { + callback(error, value); + } catch (e) { + setImmediate(rethrow, e); + } +} + +function rethrow(error) { + throw error; +} diff --git a/lib/internal/wrapAsync.js b/lib/internal/wrapAsync.js index aee0275..c4d1ea8 100644 --- a/lib/internal/wrapAsync.js +++ b/lib/internal/wrapAsync.js @@ -1,19 +1,7 @@ -import identity from 'lodash/identity'; import asyncify from '../asyncify'; var supportsSymbol = typeof Symbol === 'function'; -function supportsAsync() { - var supported; - try { - /* eslint no-eval: 0 */ - supported = isAsync(eval('(async function () {})')); - } catch (e) { - supported = false; - } - return supported; -} - function isAsync(fn) { return supportsSymbol && fn[Symbol.toStringTag] === 'AsyncFunction'; } @@ -22,6 +10,6 @@ function wrapAsync(asyncFn) { return isAsync(asyncFn) ? asyncify(asyncFn) : asyncFn; } -export default supportsAsync() ? wrapAsync : identity; +export default wrapAsync; -export { supportsAsync, isAsync }; +export { isAsync }; diff --git a/mocha_test/asyncFunctions.js b/mocha_test/asyncFunctions.js index b756a90..3b759e1 100644 --- a/mocha_test/asyncFunctions.js +++ b/mocha_test/asyncFunctions.js @@ -1,4 +1,15 @@ -var supportsAsync = require('../lib/internal/wrapAsync').supportsAsync; +var isAsync = require('../lib/internal/wrapAsync').isAsync; + +function supportsAsync() { + var supported; + try { + /* eslint no-eval: 0 */ + supported = isAsync(eval('(async function () {})')); + } catch (e) { + supported = false; + } + return supported; +} describe('async function support', function () { this.timeout(100); diff --git a/mocha_test/asyncify.js b/mocha_test/asyncify.js index a98826c..112b8ad 100644 --- a/mocha_test/asyncify.js +++ b/mocha_test/asyncify.js @@ -92,7 +92,9 @@ describe('asyncify', function(){ }); }); - it('callback error', function(done) { + it('callback error @nodeonly', function(done) { + expectUncaughtException(); + var promisified = function(argument) { return new Promise(function (resolve) { resolve(argument + " resolved"); @@ -105,11 +107,30 @@ describe('asyncify', function(){ throw new Error("error in callback"); } }); + setTimeout(function () { expect(call_count).to.equal(1); done(); }, 15); }); + + it('dont catch errors in the callback @nodeonly', function(done) { + expectUncaughtException(checkErr); + var callbackError = new Error('thrown from callback'); + + function checkErr(err) { + expect(err).to.equal(callbackError); + done(); + } + + function callback() { + throw callbackError; + } + + async.asyncify(function () { + return Promise.reject(new Error('rejection')); + })(callback); + }); } describe('native-promise-only', function() { @@ -134,5 +155,20 @@ describe('asyncify', function(){ var Promise = require('rsvp').Promise; promisifiedTests.call(this, Promise); }); + + function expectUncaughtException(onError) { + // do a weird dance to catch the async thrown error before mocha + var listeners = process.listeners('uncaughtException'); + process.removeAllListeners('uncaughtException'); + process.once('uncaughtException', function onErr(err) { + listeners.forEach(function(listener) { + process.on('uncaughtException', listener); + }); + // can't throw errors in a uncaughtException handler, defer + if (onError) { + setTimeout(onError, 0, err); + } + }); + } }); }); |