[英]How transform chain's promises into flat promise in Q and TypeScript
I want to know if there is a way to transform chain's promises in a flat promise. 我想知道是否有一种方法可以将连锁经营的承诺转化为统一的承诺。 I have this code: 我有以下代码:
import * as Q from 'q';
export class MyClass {
private methodA(): Q.Promise<boolean> {
let deferred = Q.defer<boolean>();
//some stuff, like database calls, etc
return deferred.promise;
}
private methodB(): Q.Promise<boolean> {
let deferred = Q.defer<boolean>();
//some stuff, like database calls, etc
return deferred.promise;
}
private methodC(): Q.Promise<boolean> {
let deferred = Q.defer<boolean>();
//some stuff, like database calls, etc
return deferred.promise;
}
private methodD(): Q.Promise<boolean> {
let deferred = Q.defer<boolean>();
//some stuff, like database calls, etc
return deferred.promise;
}
run(): Q.Promise<boolean> {
let deferred = Q.defer<boolean>();
let promises = [];
promises.push(this.methodA().then(wasOk => {
this.methodB().then(wasOk => {
this.methodC();
});
}));
promises.push(this.methodD());
//Wait all promises
Q.all(promises).then(wasOk => deferred.resolve(wasOk));
return deferred.promise;
}
}
This code have one problem: Q.all only is waiting to methodA and methodD; 这段代码有一个问题:Q.all只在等待methodA和methodD; and is not waiting for methodB and methodC. 并且不等待methodB和methodC。
I think I will need to put method B and C in promises's vector, or even make another vector and wait for it inside first Q.all... but it will be not a clear code, and I wondering if there a better aproach. 我想我需要将方法B和C放在promises的向量中,或者甚至制作另一个向量,并在第一个Q.all中等待它……但这不是一个清晰的代码,我想知道是否有更好的方法。
Thanks a lot! 非常感谢!
You're just missing a return
in your then
handler on methodA
, and probably within its then
handler, because you're using verbose arrow functions: 您只是错过了methodA
上的then
处理程序中的return
,并且可能在其then
处理程序中缺少了return
,因为您正在使用详细的箭头函数:
promises.push(this.methodA().then(wasOk => {
return this.methodB().then(wasOk => {
//^^^^^^^
return this.methodC();
// ^^^^^^^
});
}));
Or with concise arrow functions: 或具有简洁的箭头功能:
promises.push(this.methodA().then(wasOk => this.methodB().then(wasOk => this.methodC())));
Or with concise arrows with line breaks: 或带有带换行符的简洁箭头:
promises.push(this.methodA().then(wasOk =>
this.methodB().then(wasOk =>
this.methodC()
)
));
Note that that code does this: 请注意,该代码执行此操作:
methodA
and waits for it to resolve, then 调用methodA
并等待其解析,然后 methodB
and waits for it to resolve, then 调用methodB
并等待其解决,然后 methodC
调用methodC
So overall, the first promise in your array won't resolve until methodB
and methodC
resolve; 因此,总的来说,数组中的第一个承诺要等到方法methodB
和方法methodC
解析后methodC
解决。 methodD
is called right away and so could resolve sooner. methodD
被调用,因此可以更快地解决。
The array construction could be simpler as well: 数组构造也可能更简单:
promises = [
this.methodA().then(wasOk =>
this.methodB().then(wasOk =>
this.methodC()
)
),
this.methodD()
];
You have a slight error, almost a typo: 您有一个小错误,几乎是一个错字:
promises.push(this.methodA().then(wasOk => {
this.methodB().then(wasOk => {
this.methodC();
});
}));
It's the curly braces -- they change a return of a promise into a return of undefined. 这是花括号-它们将promise的返回更改为undefined的返回。 You could do this: 您可以这样做:
promises.push(this.methodA().then(wasOk =>
this.methodB().then(wasOk =>
this.methodC();
);
));
or this: 或这个:
promises.push(this.methodA().then(wasOk => {
return this.methodB().then(wasOk => {
return this.methodC();
});
}));
or even this: 甚至这个:
promises = [this.methodA(), this.methodB(), this.methodC()];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.