diff --git a/index.js b/index.js index 6936199..6d2ea5a 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ 'use strict'; -var once = require('once'); var isPromise = require('is-promise'); var promiseResolver = require('promise-resolver'); @@ -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; } }; diff --git a/package.json b/package.json index 3a74429..3d70e14 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test.js b/test.js index a4c4efe..9acf013 100644 --- a/test.js +++ b/test.js @@ -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; @@ -134,6 +134,6 @@ describe('runAsync', function () { assert.throws(function () { runAsync(returns)(); - }, /No Native Promise Implementation/); + }, /No Promise Implementation/); }); });