Make is possible to explicitly name callbacks (this time with working tests)#4
Make is possible to explicitly name callbacks (this time with working tests)#4alexbirkett wants to merge 3 commits into
Conversation
|
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 ? |
|
Thanks for taking the time to look at this. I take your point about the ugly API with the undefined. However, this suggestion: 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: but for me, it's not readable enough. |
The programmer does not get to choose the names of the callback functions when using async e.g: