Created
November 28, 2013 16:37
-
-
Save spiralx/7694695 to your computer and use it in GitHub Desktop.
Quick promise example
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 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