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

Stricter type checking #844

Merged
merged 2 commits into from Jul 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/async.js
Expand Up @@ -62,6 +62,12 @@
return _toString.call(obj) === '[object Array]';
};

// Ported from underscore.js isObject
var _isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};

function _isArrayLike(arr) {
return _isArray(arr) || (
// has a positive integer length property
Expand Down Expand Up @@ -1013,7 +1019,7 @@
function _console_fn(name) {
return _restParam(function (fn, args) {
fn.apply(null, args.concat([_restParam(function (err, args) {
if (typeof console !== 'undefined') {
if (typeof console === 'object') {
if (err) {
if (console.error) {
console.error(err);
Expand Down Expand Up @@ -1186,7 +1192,7 @@
return callback(e);
}
// if result is Promise object
if (typeof result !== 'undefined' && typeof result.then === "function") {
if (_isObject(result) && typeof result.then === "function") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could also be looser here and do result != null currently this will error if result is null tho

result.then(function(value) {
callback(null, value);
}).catch(function(err) {
Expand All @@ -1199,11 +1205,11 @@
};

// Node.js
if (typeof module !== 'undefined' && module.exports) {
if (typeof module === 'object' && module.exports) {
module.exports = async;
}
// AMD / RequireJS
else if (typeof define !== 'undefined' && define.amd) {
else if (typeof define === 'function' && define.amd) {
define([], function () {
return async;
});
Expand Down
11 changes: 11 additions & 0 deletions test/test-async.js
Expand Up @@ -4235,6 +4235,17 @@ exports['asyncify'] = {
});
},

'asyncify null': function (test) {
var parse = async.asyncify(function() {
return null;
});
parse("{\"a\":1}", function (err, result) {
test.ok(!err);
test.ok(result === null);
test.done();
});
},

'variable numbers of arguments': function (test) {
async.asyncify(function (x, y, z) {
test.ok(arguments.length === 3);
Expand Down