4

I'm using Angular5 in my project. I implemented methods in my api service to return promises

method(): Promise<any> {
    return new Promise<any>((resolve,reject) => {
        this.http.get(url).subscribe((response) => {
            resolve(response);
        }, (error) => reject(error));
    });
}

In component there's need to ensure that all data have been fetched before further operations. I tried to do this:

const fetch = [];
fetch.push(this.api.method());
fetch.push(this.api.method1());
fetch.push(this.api.method2());

Promise.all(fetch).then(() => alert('data fetched')); 

It never reached alert. All gets succeded with code 200.

When I paused execution at line with Promise.all i saw that my Promise(s) were converted into ZoneAwarePromise.

It works well when I just chain them nesting calls in .then(s)

Why Promise got converted? How to get Promise.all working?

1 Answers1

0

What about to use forkJoin instead of Promise.all? by doing something like:

forkJoin(...fetch.map(yourPromis(url)))
sarea
  • 215
  • 1
  • 13