Skip to content

Instantly share code, notes, and snippets.

@spiralx
Created November 28, 2013 16:37
Show Gist options
  • Save spiralx/7694695 to your computer and use it in GitHub Desktop.
Save spiralx/7694695 to your computer and use it in GitHub Desktop.
Quick promise example
var Q = require("q");
var FS = require("fs");
function process_file(fn) {
var deferred = Q.defer();
FS.readFile(fn, "utf-8", function(error, content) {
if (error) {
deferred.reject(new Error(error));
}
// do processing to content
deferred.resolve();
});
return deferred.promise;
}
Q.all([
process_file(file_a),
process_file(file_b),
process_file(file_c)
])
.then(function(results) {
console.log("All calls completed without error");
})
.fail(function(error) {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment