0

I have a function that uploads data to a server that I need to modify to upload data by chunks.

The original function implementation is the following :

    private async updateDatasource(variableName: string, variableRecords: ChartDataResponse[]):Promise<boolean> {
    // unrelated code
        return this.portalService.updateDataForChart(variableId, variableRecords)
            .then((updateRes: boolean) => {
                 if (updateRes) {
                    return this.executeRequest<HealthDataSource, boolean>({
                      path: `/variable/user/datasources/${dataSource.identifier}`,
                      method: 'PUT',
                      body: {
                        libelle: dataSource.datasource.libelle,
                        type: dataSource.datasource.type,
                        lastSyncDate: Math.max(maxDate, dataSource.datasource.lastSyncDate)
                      },
                      headers: this.getHeaders()
                    });
                  } else {
                    return false;
                  }
                });
            } else {
              return Promise.reject(false);
            }
          }

I have tried the following but I can't seem to know how to return a promise with the result on it:

    private async updateDatasource(variableName: string, variableRecords: ChartDataResponse[]): Promise<boolean> {
    //unrelated code 
    //chunked the data
          var chunks = _.chunk(variableRecords, 30);

          return _.forEach(chunks, (chunk) => this.portalService.updateDataForChart(variableId, chunk))
            .then((updateRes: boolean) => {
              if (updateRes) {
                return this.executeRequest<HealthDataSource, boolean>({
                  path: `/variable/user/datasources/${dataSource.identifier}`,
                  method: 'PUT',
                  body: {
                    libelle: dataSource.datasource.libelle,
                    type: dataSource.datasource.type,
                    lastSyncDate: Math.max(maxDate, dataSource.datasource.lastSyncDate)
                  },
                  headers: this.getHeaders()
                });
              } else {
                return false;
              }
            });
        } else {
          return Promise.reject(false);
     }
    }
abdullahalali
  • 396
  • 2
  • 5
  • 18
R0b0t0
  • 390
  • 6
  • 20

1 Answers1

1

You can get list of promise results with Promise.all()

So in your case instead of using forEach function, you want to generate array of promises:

Promise.all(
   chunks.map(chunk => this.portalService.updateDataForChart(variableId, chunk)))...
).then(results => {
   // iterate over results array and do other stuff
})
Vololodymyr
  • 1,996
  • 5
  • 26
  • 45
  • is there a way I Can create a promise for each chunk and chain the promisses ? since the Promise.all() method wait for all the requests to complete at the same time resulting in a timeout – R0b0t0 Nov 29 '19 at 10:13
  • for sure, you can follow this https://stackoverflow.com/questions/44955463/creating-a-promise-chain-in-a-for-loop – Vololodymyr Nov 30 '19 at 16:49