0

I'll like to see if there is a name(it's a bad practice?) for this:

var callback = function(error, data) {
    var method = error ? 'handleError' : 'handleSuccess';
    object[method](error || data, action);
};

If there's an error, object.handleError will use error and action, let say I dont care about any other param after error if error

In the other hand, if there's no error, object.handleSuccess will use data and action

lesterzone
  • 233
  • 3
  • 6

1 Answers1

2

It's a null-coalescing (Elvis) operation. It's not bad practice but in this case I think it is cleaner to write out the if/else since you are doing the comparison check twice anyways. This way you can omit the second param too.

var callback = function(error, data) {
    if(error){
        object.handleError(error);
    else{
        object.handleSuccess(data,action);
    }
};
Community
  • 1
  • 1
  • 1
    If I'm not mistaken, his intention is to ONLY pass along the error object in the first case, and ignore the remaining arguments. – KoratDragonDen Oct 31 '14 at 20:13
  • Thanks @Austin, just to remove the else statement, you think it will be better `var callback = function(error, data) { if(error){ return object.handleError(error); } return object.handleSuccess(data,action); };` – lesterzone Oct 31 '14 at 20:33