Skip to content

Commit

Permalink
bump promise-resolver, and simplify implementation
Browse files Browse the repository at this point in the history
I released [email protected] a bit prematurely, and have already
cut 3.0.0. It now handles a number of corner cases we were handling
manually:

 1. Ensure callbacks are only called once
 2. Require a promise implementation if no callback is provided
 3. Ensure callbacks are called async

And a few we were missing:

 1. Look for `bluebird` and fallback to native promises
 2. Supress `unhandledRejection` notices on promises if user provides a callback (assumes the callback handles errors).
 3. Consistent behavior if they return a promise that is rejected with `null` / `undefined`
 4. Minor performance boost by only binding deferred.resolve/deferred.reject methods as needed

Closes #12
  • Loading branch information
jamestalmage committed Oct 19, 2015
1 parent 58ca11e commit 4a9bc23
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
21 changes: 6 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var once = require('once');
var isPromise = require('is-promise');
var promiseResolver = require('promise-resolver');

Expand All @@ -19,35 +18,27 @@ var promiseResolver = require('promise-resolver');
module.exports = function (func, 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');
}
var deferred = promiseResolver.defer(cb);

try {
var answer = func.apply({
async: function () {
async = true;
return cb;
return deferred.cb;
}
}, Array.prototype.slice.call(arguments));

if (!async) {
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;
}
};
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
"homepage": "https://github.com/SBoudrias/run-async",
"dependencies": {
"is-promise": "^2.1.0",
"once": "^1.3.0",
"promise-resolver": "^2.0.0"
"promise-resolver": "^3.0.0"
},
"devDependencies": {
"bluebird": "^2.10.2",
"mocha": "^2.3.3"
"mocha": "^2.3.3",
"pinkie": "^1.0.0"
}
}
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var assert = require('assert');
var runAsync = require('./index');

describe('runAsync', function () {
var Promise = require('bluebird');
var Promise = require('pinkie');

it('run synchronous method', function (done) {
var ranAsync = false;
Expand Down Expand Up @@ -134,6 +134,6 @@ describe('runAsync', function () {

assert.throws(function () {
runAsync(returns)();
}, /No Native Promise Implementation/);
}, /No Promise Implementation/);
});
});

0 comments on commit 4a9bc23

Please sign in to comment.