Skip to content

Instantly share code, notes, and snippets.

@sporto
Created August 23, 2013 09:27
Show Gist options
  • Save sporto/6317306 to your computer and use it in GitHub Desktop.
Save sporto/6317306 to your computer and use it in GitHub Desktop.
Best practice on return values from module that handles http
var myModule = require('myModule');
myModule.fetch('some/url', function (err, val) {
//if 'some/url' cannot be found what should myModule return?
//some options I can think of
//#1
//err => null
//val => 404
//#2
//err => 404
//val => null
//#3
//err => A proper JS Error
//val => null
});
@sidorares
Copy link

#3, 404 status in error object

@chrisdewar
Copy link

if you are looking for a 1, 2, or 3... i'm inclined to say 3, because of the fs api... even if the filesystem (read: http) is working properly, a missing file results in an error.

if you are looking for "best practice" however, the http.get api returns an object that you can then .on('error', function (err) {});

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