Skip to content

Instantly share code, notes, and snippets.

@watson
Created October 28, 2015 21:49
Show Gist options
  • Save watson/f1bc2845e500e381d25a to your computer and use it in GitHub Desktop.
Save watson/f1bc2845e500e381d25a to your computer and use it in GitHub Desktop.
Async/delayed/cached
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) {
// ...
})
})
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