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

each() fails to check for undefined array #610

Closed
rogerfoxcroft opened this issue Sep 1, 2014 · 1 comment
Closed

each() fails to check for undefined array #610

rogerfoxcroft opened this issue Sep 1, 2014 · 1 comment

Comments

@rogerfoxcroft
Copy link

This is possible a feature request, rather than a bug, but I think async should be more tolerant of passing in empty collections. This allows more easy chaining of async.each() and other calls as part of other frameworks, where those frameworks may pass an undefined array.

Here's a code fragment which demonstrates what I mean:

var async = require('async');
var emptyArray = undefined;
async.each(emptyArray, function(e) { /* do stuff */ }, function() { console.log("All done"); });

This raises the following error:

/Users/roger/tmp/node_modules/async/lib/async.js:120
if (!arr.length) {
^
TypeError: Cannot read property 'length' of undefined
at Object.async.each (/Users/roger/tmp/node_modules/async/lib/async.js:120:17)
at Object. (/Users/roger/tmp/app.js:5:7)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3

The code can be improved, thus:

var async = require('async');
var emptyArray = undefined;
if (emptyArray) async.each(emptyArray, function(e) { /* do stuff */ }, function() { console.log("All done"); });

but this feels like unnecessary boiler plating for a check which could easily be incorporated into async.js.

@rogerfoxcroft
Copy link
Author

This is a dupe of #578.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant