I am currently working on updating a function that sends data to a server, and I need to modify it so that it can upload the data in chunks.
The original implementation of the function is as follows:
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 attempted the following approach, but I am struggling with how to properly return a promise with the desired result:
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);
}
}