简体   繁体   English

Angular2 $ http承诺与Promise.all()

[英]Angular2 $http promises with Promise.all()

I'm triying to implement a simple promise pattern in angular2. 我试图在angular2中实现一个简单的promise模式。 The idea is execute a block of code when the two promise get resolved. 这个想法是当两个诺言得到解决时执行一段代码。

The approach is with Promise.all() and passing there the promises in my service: 该方法与Promise.all()并在其中传递我的服务中的承诺:

predict(data): Promise<any> {
    let headers    = new Headers({'Content-Type': 'application/json'});
    let dataString = JSON.stringify(data);

    return this.http
      .post(`${this.baseUrl}/predict`, dataString, headers)
      .toPromise()
      .then(
        (res: Response) => Promise.resolve(res.json())
      )
      .catch(
        (err) => Promise.reject(err)
      );
  } // predict

And the component controller: 和组件控制器:

promiseOne = this.apiservice.predict(titleToPredict);
promiseTwo = this.apiservice.predict(secondTitleToPredict);

Promise.all(promiseOne, promiseTwo)
   .then(data => console.log(data))
   .catch(err => console.log(err));

But TypeScript Returns me Supplied parameters do not match any signature of call target. 但是TypeScript向我返回Supplied parameters do not match any signature of call target.

As i know, the methods are promises, so... i can't figure what is the problem. 据我所知,这些方法是有前途的,所以...我不知道是什么问题。

Should be... 应该...

Promise.all([promiseOne, promiseTwo])

... as this method takes one argument - an Iterable (such as an Array). ...,因为此方法需要一个参数-Iterable (例如Array)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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