node.js Q.js - 使用延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12505850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Q.js - Using deferred
提问by Chris Abrams
How do I get the value of the text from the example below?
如何从下面的示例中获取文本的值?
Q.js has an example on using Deferred:
Q.js 有一个使用 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:
在这种情况下,使用了一个节点异步函数。我想要做的是从返回的 deferred.promise 中获取文本的值。当我 console.log(deferred.promise) 我得到这个:
{ 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?
我做错了什么(因为我只是从这里复制/粘贴了示例:https: //github.com/kriskowal/q#using-deferreds)或者我还需要做什么才能从文件中实际获取该文本?
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.
我知道 node.js 有上面调用的同步版本 - 我的目标是了解 deferred 如何与这个库一起工作。
采纳答案by Jonathan Lonowski
You can get the value via the .then()methodof a Promise:
您可以通过Promise的.then()方法获取值:
function read() {
// your snippet here...
}
read().then(function (text) {
console.log(text);
});
Also, error handlers can be passed either as a 2nd argument to .then()or with the .fail()method:
此外,错误处理程序可以作为第二个参数传递给方法.then()或使用.fail()方法:
read().fail(function (err) {
console.log(err);
});
回答by vaughan
See https://github.com/kriskowal/q#adapting-node
见https://github.com/kriskowal/q#adapting-node
Can be rewritten in a nodejs-like:
可以用类似 nodejs 的方式重写:
var read = Q.nfcall(FS.readFile, FS, "foo.txt", "utf-8");
read().then( function (text) { console.log(text) } );
回答by Mariusz Nowak
deferred.promise.then(function (text) {
console.log(text); // Bingo!
});
回答by vinayr
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.
奇怪的是你看不到console.log(foo). 不知道为什么。
Check more examples here https://github.com/kriskowal/q/wiki/Examples-Gallery
在此处查看更多示例https://github.com/kriskowal/q/wiki/Examples-Gallery
回答by Vietnhi Phuvan
Q = require('q');
FS = require('fs');
var deferred = Q.defer();
FS.readFile("client-02.html", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
return deferred.promise.done( setTimeout(console.log(text),1000 ));
});

