0

I can't make the Promise.all() wait for the items. Below are the codes that I used.

PLEASE NOTE THAT THIS IS NOT A DUPLICATE

const items = Object.keys(this.models).map(key => {
  return this.processThenCallback(key).subscribe(() => {
    this.populateItems(key);
    return this.getOtherItems(key);
  });
});
Promise.all(items).then(() => {
  // Perform important function
});

I used import 'rxjs/add/operator/map' to return an Observable;

private processThenCallback(key) {
    return this.someService.getThings().map(data => {
        // Do something on the data
    ));
}

private populateItems(key) {
    // Perform synchronous process, nothing special
    return key;
}

private getOtherItems(key) {
    return this.otherService.getOtherItems().map(data => {
        // Do something on data
    ));
}
iPhoneJavaDev
  • 821
  • 5
  • 33
  • 78
  • I think the answer provided by CertainPerformance does not apply to my question. – iPhoneJavaDev Mar 26 '19 at 07:47
  • 1
    It does. You need to change `processThenCallback` so that it returns a promise. That probably also means that you should remove the `callback` parameter and change `getOtherItems` into a promise-returning function as well. – Bergi Mar 26 '19 at 07:51
  • But i need `populateItems` and `getOtherItems` to run only after `processThenCallback`. I'm not really good in promises cannot even think of how to do it. – iPhoneJavaDev Mar 26 '19 at 07:54
  • 1
    The duplicate explains how to create a promise that resolves when a callback happens. Please try to write versions of `process` and `getOtherItems` that do this. [Edit] your question to include the attempt. We'll see how to combine them in the `Promise.all` later. – Bergi Mar 26 '19 at 07:59
  • Ok, i'll try it now. – iPhoneJavaDev Mar 26 '19 at 07:59
  • I updated the functions to return an Observable. – iPhoneJavaDev Mar 26 '19 at 08:43
  • Updated again the top-most code – iPhoneJavaDev Mar 26 '19 at 08:52
  • You need to create promises, not observables, for `Promise.all` to work. Also, that `subscribe` in the top code should be replaced as well - either with `flatMap` (when working with observables) or with `then` (when already having promises). – Bergi Mar 26 '19 at 08:58
  • By the way, the services are actually http service from angular that returns an Observable, that's why I use Observable – iPhoneJavaDev Mar 26 '19 at 09:24
  • Try using `toPromise().then` instead of `pipe` – Bergi Mar 26 '19 at 10:04
  • didn't work. still the function inside the method `getOtherItems()` didn't run – iPhoneJavaDev Mar 26 '19 at 10:17
  • Did you call `toPromise()` on that as well? – Bergi Mar 26 '19 at 10:19
  • Actually that toPromise is showing errors in my terminal while compiling – iPhoneJavaDev Mar 26 '19 at 10:28
  • Can you update the code in your question to show what you've tried? Also, *what* errors is it showing? – Bergi Mar 26 '19 at 11:21
  • 1
    It works now. I just change to `getOtherItems().toPromise()` together with `return this.processThenCallback(key).toPromise().then`... thank you so much, you've been a great help!!! – iPhoneJavaDev Mar 27 '19 at 05:55

0 Answers0