-
Notifications
You must be signed in to change notification settings - Fork 51
Description
In this readme, these two examples are provided:
Intro
var myURL;
Bro(app)
.iDontAlways('config.environment.buildURL')
.butWhenIdo(function(buildURL){
myURL = buildURL('dev');
});
Calling nested functions
Bro(object)
.iDontAlways('method')
.butWhenIdo(function(returnVal) {
console.log('object.method() returned ', returnVal);
});
As far as I can tell, both are functionally identical and call a nested method. In the first example, the function is passed as a callback inside butWhenIdo and then called, whereas in the second example, the return value of the function is passed.
Looking at the code, this looks like the pertinent section:
if (returnValue) {
(callback || function(){}).call(context || this.object, returnValue);
}
Am I correct in interpreting this that if the nested function returns a value, the return value is passed to the callback, and if it does not return a value, the function is passed to the callback?
If so, that seems like it may be of risky design - you're equally likely to call the function twice by accident as you are to expect it to be a function and instead have it be the returned value? If not, perhaps the readme can be improved to clarify the behavior?