Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncify promisified functions #840

Merged
merged 12 commits into from
Jul 9, 2015
11 changes: 10 additions & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,16 @@
} catch (e) {
return callback(e);
}
callback(null, result);
// if result is Promise object
if (typeof result !== 'undefined' && typeof result.then === "function") {
result.then(function(value) {
callback(null, value);
}).catch(function(err) {
callback(err.message ? err : new Error(err));
});
} else {
callback(null, result);
}
});
};

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
"license": "MIT",
"devDependencies": {
"benchmark": "bestiejs/benchmark.js",
"bluebird": "^2.9.32",
"coveralls": "^2.11.2",
"es6-promise": "^2.3.0",
"jscs": "^1.13.1",
"jshint": "~2.8.0",
"lodash": "^3.9.0",
"mkdirp": "~0.5.1",
"native-promise-only": "^0.8.0-a",
"nodeunit": ">0.0.0",
"nyc": "^2.1.0",
"rsvp": "^3.0.18",
"uglify-js": "~2.4.0",
"xyz": "^0.5.0",
"yargs": "~3.9.1"
Expand Down
50 changes: 49 additions & 1 deletion test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -4267,5 +4267,53 @@ exports['asyncify'] = {
test.ok(e.message === "callback error");
test.done();
}
}
},

'promisified': [
'native-promise-only',
'bluebird',
'es6-promise',
'rsvp'
].reduce(function(promises, name) {
if (isBrowser()) {
// node only test
return;
}
var Promise = require(name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to make these tests node-only, because this won't work in the browser.

if (typeof Promise.Promise === 'function') {
Promise = Promise.Promise;
}
promises[name] = {
'resolve': function(test) {
var promisified = function(argument) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(argument + " resolved");
}, 15);
});
};
async.asyncify(promisified)("argument", function (err, value) {
if (err) {
return test.done(new Error("should not get an error here"));
}
test.ok(value === "argument resolved");
test.done();
});
},

'reject': function(test) {
var promisified = function(argument) {
return new Promise(function (resolve, reject) {
reject(argument + " rejected");
});
};
async.asyncify(promisified)("argument", function (err) {
test.ok(err);
test.ok(err.message === "argument rejected");
test.done();
});
}
};
return promises;
}, {})
};