[英]promise Q not working consistently
Im using the Q library of promise and I've some basic question I've created this code and when I omit the first return Q word I got error (return Q(100)),but for the second and the third there is no problem if I omit them, the code is still working,why? 我正在使用promise的Q库,我有一个基本的问题我已经创建了此代码,当我省略第一个返回Q字时,我得到了错误(返回Q(100)),但是对于第二个和第三个,则没有如果我忽略它们,问题仍然存在,为什么?
function firstFn() {
return Q(100);
};
function secFn(){
return Q(200);
};
function thirdFn(){
return q(300);
};
firstFn().then(function(a){
alert(a);
return secFn();
}).then(function(b){
alert(b);
return thirdFn();
}).then(function(c){
alert(c)
});
You are invoking .then(function(a)...) on the value returned from firstFn so it needs to return something that has a .then function on it. 您正在对从firstFn返回的值调用.then(function(a)...),因此它需要返回上面带有.then函数的内容。
For the the other 2 functions, you are invoking them within a Q .then() callback. 对于其他两个函数,您正在Q .then()回调中调用它们。 If you return a promise from those callbacks, Q will take advantage of them but if you don't return anything, Q is ok with that too.
如果您从这些回调中返回承诺,则Q将利用它们,但如果您不返回任何内容,则Q也可以。
To elaborate on what Robert Levy has said: 详细阐述罗伯特·利维说的话:
The Promises/A+ specifiaction which specifies how Q's then works dictates that whenever a value is returned from a .then
callback it is unwrapped as a promise. Promises / A +规范指定了Q的工作方式,该规范规定,只要从
.then
回调返回值.then
就将其作为承诺进行包装。
What calling Q(foo)
does, which is similar to what Promise.resolve(foo)
does is wrap things as a promise. 调用
Q(foo)
所做的与Promise.resolve(foo)
所做的相似,是将事物包装为一个承诺。 So Q(100) is a promise over the value 100. Whenver a value is returned from a then
callback, it is "wrapped". 因此Q(100)是对值100的承诺。每当从
then
回调返回一个值时,它都会被“包装”。
Q(100);
Q().then(function(){ return 100; }); // the same as the line above
Q.try(function(){ return 100; }); // same as line above
// or in ES6 native promises or bluebird promises
Promise.resolve(100);
Promise.resolve().then(function(){ return 100; }); // the same as the line above
To be perfectly clear, the spec says about the .then
callback arguments: 明确地说,该规范说明了
.then
回调参数:
If either
onFulfilled
oronRejected
returns a value x, run the Promise Resolution Procedure [[Resolve]](promise2, x).如果
onFulfilled
或onRejected
返回值x,请运行Promise Resolution Procedure [[Resolve]](promise2,x)。
The Promise Resolution Procedure states: 承诺解决程序指出:
If x is a promise, adopt its state [3.4]: (note mine: not our case)
如果x是一个承诺,则采用其状态[3.4] :(请注意:不是我们的情况)
Otherwise, if x is an object or function,
否则,如果x是对象或函数,
Let
then
bex.then
.then
让x.then
。 [3.5][3.5]
if
then
is not a function, fulfill promise with x如果
then
不是函数,则用x履行承诺
So, Q is OK in doing this, this makes our life easier and allows us to assimilate values as promises. 因此,Q这样做是可以的,这使我们的生活更轻松,并使我们能够像承诺一样吸收价值。 The reasoning is that we want to keep the appearance of
.then
chains async, so returning a value is really just fulfilling the the new promise we're wrapping 原因是我们希望保持
.then
链的外观异步,因此返回值实际上只是在履行我们包装的新承诺
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.