Nano Nano - 1 year ago 142
AngularJS Question

Angular2 $http promises with Promise.all()

I'm triying to implement a simple promise pattern in angular2. The idea is execute a block of code when the two promise get resolved.

The approach is with

and passing there the promises in my service:

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)
(res: Response) => Promise.resolve(res.json())
(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.

As i know, the methods are promises, so... i can't figure what is the problem.

Answer Source

Should be...

Promise.all([promiseOne, promiseTwo])

... as this method takes one argument - an Iterable (such as an Array).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download