简体   繁体   English

如何在Q和TypeScript中将链式承诺转化为统一承诺

[英]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: 请注意,该代码执行此操作:

  • Calls methodA and waits for it to resolve, then 调用methodA并等待其解析,然后
  • Calls methodB and waits for it to resolve, then 调用methodB并等待其解决,然后
  • Calls 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM