Skip to content

Instantly share code, notes, and snippets.

@sjlu
Created September 23, 2015 14:27
Show Gist options
  • Save sjlu/6fc4101a2f8774878142 to your computer and use it in GitHub Desktop.
Save sjlu/6fc4101a2f8774878142 to your computer and use it in GitHub Desktop.

Lets take an example where we have to load a dyanmic set of files. Normally we would have to iterate through the array, grab one file, then the next, etc. PHP is a good example of this and it'd take it a really long time to access each file since the computer's disk is slower than the CPU. This is what we call I/O wait.

$files = array("file1.txt", "file2.txt")
for ($i = 0; $i < count($files); $i++) {
  $fh = fopen($myFile, 'r');
  $theData = fread($fh, filesize($myFile));
  fclose($fh);
  echo $theData;
}

Now Node.js and Javascript kind of solve this problem, where we can rely on callbacks. However, when we have a unknown number of files, we need to write some logic in order for a callback function to complete. The code to this is realatively easy but is also pretty tedious to write especially when you're doing it all the time

var fs = require('fs')

var files = ["text1.txt", "text2.txt"];
var fileData = []
var cb = function(err, data) {
  fileData.push(data)
  if (fileData.length >= files.length) {
    // all done
  }
}
files.each(function(file) {
  fs.readFile(file, cb)
})

In the above case, we're executing the callback function all the time where we don't necessarily need to. Promises can help solve this, a good example would be

var Promise = require('bluebird')
var fs = Promise.promisifyAll(require('fs')) 
// this helps us create "promisified" functions so all functions 
// it provides will now will end in "Async" which means its a 
// promisified version of that function

Promise
  .resolve(["file1.txt", "file2.txt"]) // this just passes data to the next function
  .map(function(file) {
    return fs.readFileAsync(file)
  })
  .then(function(fileData) {
    // finished
  })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment