Skip to content

Instantly share code, notes, and snippets.

@alexindigo
Last active November 4, 2016 23:19
Show Gist options
  • Save alexindigo/e9655d78e6f2d4aeef1980304f594e99 to your computer and use it in GitHub Desktop.
Save alexindigo/e9655d78e6f2d4aeef1980304f594e99 to your computer and use it in GitHub Desktop.
callbackMethod(input, function myHandler(error, result) {
if (error)
{
// check for error, since it's in my face here as first argument
return;
}
// do other things
});
// vs
promiseMethod(input).then(function halfOfMyHandler(result) {
// looks like I forgot to check for errors at all
// just do things
});
// vs
promiseMethod(input).then(function halfOfMyHandler(result) {
// just do things
}).catch(function anotherHalfOfMyHandler(error) {
// so why do I need to write two functions? o_0
});
@alexindigo
Copy link
Author

From: http://paste.ubuntu.com/23427957/

class Service {
  businessLogic(result) {
    //do something
  }
  
  errorHandler(error) {
    if (error) {
      return;
    }
  }
}

var sevice = new Srvice();

promiseMethod(input).then(service.businessLogic}).catch(service.errorHandler);

//vs

promiseMethod(input).then(result => {
  // just do things
}).catch(error => {
  // so why do I need to write two functions? o_0
});

@alexindigo
Copy link
Author

In real life app, you'd have more than one result handler and more than one error handler, and it will be more cognitive load try to keep track if you used proper error handler for the given result handler.

I personally, not a fan of separating error handling from "business logic", since it's not pure business logic at this level (where your code touches some kind of I/O) and usually you'd keep your "true business logic" in a separate module.

var myLogic = require('./lib/my_logic.js');

callbackMethod(input, function myHandler(error, result) {
  if (error)
  {
    // check for error, since it's in my face here as first argument
    return;
  }
    
 myLogic.doThings(result.data);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment