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

Make is possible to explicitly name callbacks (this time with working tests) #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

alexbirkett
Copy link

The programmer does not get to choose the names of the callback functions when using async e.g:

    async.series([
                    function(callback)  {
                        trackerSimulator.connect({host: 'localhost', port: port}, callback);
                    },
                    function(callback) {
                        trackerSimulator.sendMessage(data, 0, 50, sliceLength, callback);
                    },
                    function(callback) {
                        trackerSimulator.waitForData(numberOfBytesToWaitFor, addTimeout(3001, callback, undefined, 'waitfordata'));
                    },
                    function(callback) {
                        waitForMessage(callback);
                    }
                   ],
                   function(err, data) {
                       var dataReceivedByClient = data[2];
                       callback(err, returnObject.message, dataReceivedByClient);
                       trackerSimulator.destroy();
                       locationIo.close(); 
                });

```javascript

@temsa
Copy link
Owner

temsa commented Apr 25, 2013

First, thanks for posting a PR, with test coverage :)

Then, well... I'm pretty sure for such a KISS library, that it would harm it rather than improve it. We probably shouldn't add something for naming the callback like this.

Moreover, the API looks quite ugly with this 'undefined', I'd rather would have kept the last parameter as the optional err handler function, and checked for the type for deciding what to do with it.

BTW, what about making some documentation instead of this code ? This aim, naming a callback, could totally be achieved through different options,

like

    async.series([
                    function(callback)  {
                        trackerSimulator.connect({host: 'localhost', port: port}, callback);
                    },
                    function(callback) {
                        trackerSimulator.sendMessage(data, 0, 50, sliceLength, callback);
                    },
                    function(callback) {
                        callback.name = 'waitfordata';
                        trackerSimulator.waitForData(numberOfBytesToWaitFor, addTimeout(3001, callback));
                    },
                    function(callback) {
                        waitForMessage(callback);
                    }
                   ],
                   function(err, data) {
                       var dataReceivedByClient = data[2];
                       callback(err, returnObject.message, dataReceivedByClient);
                       trackerSimulator.destroy();
                       locationIo.close(); 
                });

or

    async.series([
                    function(callback)  {
                        trackerSimulator.connect({host: 'localhost', port: port}, callback);
                    },
                    function(callback) {
                        trackerSimulator.sendMessage(data, 0, 50, sliceLength, callback);
                    },
                    function(callback) {
                        trackerSimulator.waitForData(numberOfBytesToWaitFor, addTimeout(3001, function waitForData() { return callback.apply(this, arguments) }));
                    },
                    function(callback) {
                        waitForMessage(callback);
                    }
                   ],
                   function(err, data) {
                       var dataReceivedByClient = data[2];
                       callback(err, returnObject.message, dataReceivedByClient);
                       trackerSimulator.destroy();
                       locationIo.close(); 
                });

What do you think ?

@alexbirkett
Copy link
Author

Thanks for taking the time to look at this. I take your point about the ugly API with the undefined.

However, this suggestion:

function(callback) {
                        callback.name = 'waitfordata';
                        trackerSimulator.waitForData(numberOfBytesToWaitFor, addTimeout(3001, callback));
                    },

is pretty, (I tried this first of all) but it does not appear to work. If you can make it work, please let me know.

I also considered this:

 function(callback) {
                        trackerSimulator.waitForData(numberOfBytesToWaitFor, addTimeout(3001, function waitForData() { return callback.apply(this, arguments) }));
                    }

but for me, it's not readable enough.

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 this pull request may close these issues.

2 participants