How do I get the value of the text from the example below?
Q.js has an example on using Deferred:
var deferred = Q.defer();
FS.readFile(\"foo.txt\", \"ut
Q = require('q');
FS = require('fs');
function qread() {
var deferred = Q.defer();
FS.readFile("foo.txt", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
});
return deferred.promise;
};
var foo = qread();
setTimeout(function() {
console.log(""+foo);
},1000);
It's strange you cannot see the output for console.log(foo)
. Dont' know why.
Check more examples here https://github.com/kriskowal/q/wiki/Examples-Gallery