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", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
});
return deferred.promise;
In this case, there is a node async function being used. What I want to do is get the value of text from the deferred.promise being returned. When I console.log(deferred.promise) I get this:
{ promiseSend: [Function], valueOf: [Function] }
What am I doing wrong (as I just copy/pasted the example from here: https://github.com/kriskowal/q#using-deferreds) or what else do I need to do to actually get that text from the file?
I am aware that node.js has a synchronous version of the call above – my goal is to understand how deferred works with this library.
You can get the value via the
.then()method of a Promise:Also, error handlers can be passed either as a 2nd argument to
.then()or with the.fail()method: