Skip to content

Instantly share code, notes, and snippets.

@flamingm0e
Forked from klovadis/gist:2549029
Created April 15, 2014 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flamingm0e/10700504 to your computer and use it in GitHub Desktop.
Save flamingm0e/10700504 to your computer and use it in GitHub Desktop.
var fs = require('fs');
// read a file
function read_the_file(filename, callback) {
// begin by reading a file
fs.readFile(filename, function (err, contents) {
// an error occurred, i.e. the file was not found.
// instead of throwing an error, skip the other
// functions and directly invoke the callback
// function and provide the error object
if (err) return callback(err);
// continue
read_data_from_db(null, contents, callback);
});
} // read_the_file()
// this function would hold the next step
function read_data_from_db(err, contents, callback) {
/* logic here */
} // read_data_from_db()
// this function call could originate from somewhere else
// in your code.
read_the_file('/some/file', function (err, result) {
// don't throw the error, just log it (just because)
if (err) {
console.log(err);
return;
}
// do something with the result
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment