Skip to content

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

Open
alexbirkett wants to merge 3 commits into
temsa:masterfrom
alexbirkett:master
Open

Make is possible to explicitly name callbacks (this time with working tests)#4
alexbirkett wants to merge 3 commits into
temsa:masterfrom
alexbirkett:master

Conversation

@alexbirkett
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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