0

in this code i want to set value in where object before processing to next line , but just after this.conn.getUserByAlterNumber the execution is gone to if (this.doctorFilter && this.doctorFilter.length) without waiting for response from the function and thus this.conn.getOrders isgetting a blank object in place of where and my filter is not working, how to stop the execution until this.conn.getUserByAlternateNumber returns value.

loadMore(offset: number): Promise<any> {
    let where: any = {};
    this.filters.forEach((item: any) => {
      if (item.key === 'alternateNumber') {
        this.conn.getUserByAlternateNumber(item.value)
          .then((user: any) => {
            where['contactName'] = user[0].attributes.PatientName;
            console.log(where,'>>>>>');
          });
      } else {
        (where[item.key] = item.value);
      }
    });
    if (this.doctorFilter && this.doctorFilter.length) {
      where.allocatedDoctor = this.doctorFilter;
    }
    console.log(where);
     return this.conn.getOrders({
      where,
      include: ['user', 'allocatedDoctor'],
      limit: this.ui.grid.pageSize,
      asc: this.OrderAsc,
      offset,
    })
      .then((items: any) => this.zone.run(() => Promise.resolve(items.map((i: any) => JSON.parse(JSON.stringify(i))))));
  }
EzLo
  • 13,780
  • 10
  • 33
  • 38
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all –  Jan 04 '19 at 11:37
  • can you please comment on what exactly I need to do in this code to make this.conn.getUserByAlternateNumber() wait until it returns – user5554873 Jan 04 '19 at 11:43
  • [Don't use `forEach` with promises](https://stackoverflow.com/a/37576787/1048572) – Bergi Jan 04 '19 at 12:12
  • Use `.map()` to turn `this.filters` into an array of Promises, then pass them to `Promise.all()` und use `.then(results => ...)` to handle the response array. –  Jan 04 '19 at 12:44

0 Answers0