javascript 使用 q.js 链接 promise
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20074685/
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
chaining promises with q.js
提问by Anon
I'm trying to understand how promise chaining works. I'm using q.js. Here's what I'm playing with.
我试图了解承诺链是如何工作的。我正在使用q.js。这就是我正在玩的东西。
var Q = require("q"); // npm install q
// the function Q(value) returns a fulfilled promise with the value... I think.
Q(1).then(Q(2)).then(Q(3)).then(Q(4)).then(function(n) {
console.log(n);
});
Q(1).then(function(n) {
return Q(2);
}).then(function(n) {
return Q(3);
}).then(function(n) {
return Q(4);
}).then(function(n) {
console.log("done: " + n);
});
My question basically boils down to why does the first one log 1
while the latter one logs what I would expect and basically logs 1 through 4. I had hoped the first one would log 4
instead of 1
.
我的问题基本上归结为为什么第一个记录1
而后一个记录我所期望的并且基本上记录 1 到 4。我曾希望第一个记录4
而不是1
.
I really just wanted to be able to have some methods that return promises and then chain them together in a waterfall like fashion - I guess I could use asyncand waterfall, but just wanted to know if this could be achieved w/ promises.
我真的只是希望能够有一些方法来返回承诺,然后将它们以瀑布式的方式链接在一起——我想我可以使用异步和瀑布式,但只是想知道这是否可以通过承诺来实现。
回答by numbers1311407
It's because then
doesn't expect another promise as an argument. Rather it expects handler functions, an callbackand/or an errback, the former you are passing in your 2nd example. Indeed any argument that is nota function is simply ignored.
这是因为then
不期望另一个承诺作为参数。相反,它需要处理函数、回调和/或errback,前者是您在第二个示例中传递的。 事实上,任何不是函数的参数都会被忽略。
从文档:
If you return a value in a handler, outputPromise will get fulfilled.
If you throw an exception in a handler, outputPromise will get rejected.
If you return a promise in a handler, outputPromise will “become” that promise. Being able to become a new promise is useful for managing delays, combining results, or recovering from errors.
如果您在处理程序中返回一个值,则 outputPromise 将得到满足。
如果在处理程序中抛出异常, outputPromise 将被拒绝。
如果您在处理程序中返回承诺, outputPromise 将“成为”该承诺。能够成为一个新的承诺对于管理延迟、组合结果或从错误中恢复很有用。
So yes, chaining promises can be done. You're doing it right in your 2nd example.
所以是的,可以完成链接承诺。您在第二个示例中做得对。
It's possible that the contrived example here of passing fulfilled promises makes the way chaining promises works seem overly verbose, but in real world usage, you typically chain promises because you're interested in their return values, e.g.:
此处传递已履行的承诺的人为示例可能会使链接承诺的工作方式显得过于冗长,但在实际使用中,您通常会链接承诺,因为您对它们的返回值感兴趣,例如:
somethingAsync().then(function (n) {
return somethingElseAsync(n);
})
.then(function (n) {
return somethingElseAsync(n);
})
.then(function (result) {
// ...
})
(Actually thismirrors async.waterfall
. If you just wanted to call a series of async functions in order with no regards to their results you could use async.series
)
(实际上这反映了async.waterfall
。如果您只想按顺序调用一系列异步函数而不考虑它们的结果,您可以使用async.series
)
回答by Dustin Getz
I know javascript doesn't have static types, but you need to think about types here.
我知道 javascript 没有静态类型,但您需要在这里考虑类型。
Q(1); // has type Promise[Int]
function (n) { return Q(2); } // has type Int => Promise[Int]
Q.then
needs the second.
Q.then
需要第二个。