-
-
Save creationix/602efd6a0d24b77fda36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test_and_load does two things. First it checks to see if a filepath | |
// is really a file and not a directory ot something crazy. | |
// If it's not a file it emits "undefined" (except for one case where null is used) | |
// If it is a file, then it emits the file's contents | |
// Any errors in fs.stat or fs.fileRead are passed through as is. | |
// In these examples, we're assuming that the fs module is of the same | |
// style as the function comsuming it. So we're showing a small example of | |
// producer and consumer for each of the styles. | |
// simple continuable with seperate callback and errback | |
function test_and_load(filename) { return function (callback, errback) { | |
fs.stat(filename)(function (stat) { | |
// Filter out non-files | |
if (!stat.isFile()) { callback(); return; } | |
// Otherwise read the file in | |
fs.readFile(filename)(callback, errback); | |
}, errback); | |
}} | |
// simple continuable with single combined callback | |
function test_and_load(filename) { return function (callback) { | |
fs.stat(filename)(function (stat) { | |
// Pass along any errors before we do anything | |
if (stat instanceof Error) { callback(stat); return; } | |
// Filter out non-files | |
if (!stat.isFile()) { callback(); return; } | |
// Otherwise read the file in | |
fs.readFile(filename)(callback); | |
}); | |
}} | |
// inimino style continuables | |
function test_and_load(filename) { return function (cont) { | |
fs.stat(filename)(rightContinuation(cont)(function (stat) { | |
// Filter out non-files | |
if (!stat.isFile()) { cont(); return; } | |
// Otherwise read the file in | |
fs.readFile(filename)(cont); | |
})); | |
}} | |
// http://github.com/bentomas/node-continuables | |
function test_and_load(filename) { | |
return fs.stat(filename)(function (stat) { | |
// Pass along any errors before we do anything | |
if(stat instanceof Error) { return; } | |
// Filter out non-files | |
if (!stat.isFile()) { return null; } | |
// Otherwise read the file in | |
return fs.readFile(filename); | |
}); | |
} | |
// promise based with node-promise helpers (github.com/kriszyp/node-promise) | |
var when = require("promise").when; | |
function test_and_load(filename) { | |
return when(fs.stat(filename), function (stat) { | |
// Filter out non-files | |
if (!stat.isFile()) { return; } | |
// Otherwise read the file in | |
return fs.readFile(filename); | |
}); | |
} | |
// promise based with CommonJS monkey patch (github.com/kriszyp/node-commonjs/blob/master/patch-promise.js) | |
function test_and_load(filename) { | |
return fs.stat(filename).then(function (stat) { | |
// Filter out non-files | |
if (!stat.isFile()) { return; } | |
// Otherwise read the file in | |
return fs.readFile(filename); | |
}); | |
} | |
// promise based (how it works in current node) | |
function test_and_load(filename) { | |
var promise = new process.Promise(); | |
fs.stat(filename).addCallback(function (stat) { | |
// Filter out non-files | |
if (!stat.isFile()) { promise.emitSuccess(); return; } | |
// Otherwise read the file in | |
fs.readFile(filename).addCallback(function (data) { | |
promise.emitSuccess(data); | |
}).addErrback(function (error) { | |
promise.emitError(error); | |
}); | |
}).addErrback(function (error) { | |
promise.emitError(error); | |
}); | |
return promise; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment