Created
October 28, 2015 21:49
-
-
Save watson/f1bc2845e500e381d25a to your computer and use it in GitHub Desktop.
Async/delayed/cached
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
var files = ['foo.js', 'bar.js', 'foo.js'] | |
files.forEach(function (file) { | |
// this will end up reading foo.js twice | |
fs.readFile(file, function (err, data) { | |
// ... | |
}) | |
}) |
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
var files = ['foo.js', 'bar.js', 'foo.js'] | |
var readFile = magic(function (file, cb) { | |
fs.readFile(file, cb) | |
}) | |
files.forEach(function (file) { | |
// this will start reading foo.js and bar.js right away, | |
// but wait reading the 2nd foo.js and just use the cached | |
// result of the first foo.js | |
readfile(file, function (err, data) { | |
// ... | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment