【发布时间】:2014-05-05 21:28:54
【问题描述】:
这可能是一个菜鸟问题,但我是新来的承诺,并试图弄清楚如何在 node.js 中使用Q。
我看到tutorial 以
开头promiseMeSomething()
.then(function (value) {}, function (reason) {});
但我无法理解.then 的确切来源。我猜它来自
var outputPromise = getInputPromise()
.then(function (input) {}, function (reason) {});
但是getInputPromise() 来自哪里?我发现以前没有提到它。
我已经将它包含在我的项目中
var Q = require('q');
// this is suppose, the async function I want to use promise for
function async(cb) {
setTimeout(function () {
cb();
}, 5000);
}
async(function () {
console.log('async called back');
});
在我的示例中如何使用Q 及其.then?
【问题讨论】:
-
你的例子不是一个很好的 Promise 用例。
then函数附加到 Promise 上,是的,需要一些东西来创建该 Promise。有时图书馆会这样做。这是一个与 MySQL 相关的示例,但您可能会发现 an answer I gave someone 很有帮助,我在其中使用普通回调然后使用 Q 承诺来实现答案,并对 Q 部分进行大量评论以说明正在发生的事情。
标签: javascript node.js promise q