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

whilst crashes if callback function is not given #446

Closed
soichih opened this issue Feb 15, 2014 · 2 comments
Closed

whilst crashes if callback function is not given #446

soichih opened this issue Feb 15, 2014 · 2 comments

Comments

@soichih
Copy link

soichih commented Feb 15, 2014

Following code

var async = require('async');
async.whilst(
function() { return false; },
function() {console.log('hi');}
);

Will results in

/home/hayashis/git/osg-blast/node_modules/async/lib/async.js:627
            callback();
            ^
TypeError: undefined is not a function

If I don't need to do anything after the loop, it's natural to assume that I should be able to not supply any callback function.

@coreyjewett
Copy link
Contributor

I agree that there is a natural use case for not using the callback. Two reasons I don't think it should be allowed though:

  1. This seems like an uncommon use case. I would prefer that Async continue with the current behavior to prevent accidental black holes.
  2. If an err is thrown in the test, what happens to it.

I suppose that those are the same issue. Thoughts?

Sent from my Samsung Galaxy S®4

-------- Original message --------
From: Soichi Hayashi [email protected]
Date:02/14/2014 16:04 (GMT-08:00)
To: caolan/async [email protected]
Subject: [async] whilst crashes if callback function is not given (#446)

Following code

var async = require('async');
async.whilst(
function() { return false; },
function() {console.log('hi');}
);
Will results in

/home/hayashis/git/osg-blast/node_modules/async/lib/async.js:627
callback();
^
TypeError: undefined is not a function
If I don't need to do anything after the loop, it's natural to assume that I should be able to not supply any callback function.


Reply to this email directly or view it on GitHub.

@soichih
Copy link
Author

soichih commented Feb 15, 2014

Humm really? I am really surprised.

All callback functions should be optional unless it's absolutely needed (I can't think of any such case, however). For example, all (as far as I know) callback functions given to node's fs package are optional.

You can do something like...

fs.readFile('test.js');

or

fs.readDir('.');

It might look odd, but maybe user just wants to see if fs.readFile will throw any exception? Who knows.

In case of async.whilst, it is perfectly normal that no post-loop processing is needed. I strongly recommend that you make the last callback function optional to make it more consistent with the rest of the standard node libraries.

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

Successfully merging a pull request may close this issue.

3 participants