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);
}
}