【发布时间】:2019-11-13 08:28:31
【问题描述】:
我有一个将数据上传到服务器的功能,我需要修改它以分块上传数据。
原函数实现如下:
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);
}
}
我尝试了以下方法,但似乎不知道如何返回带有结果的承诺:
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);
}
}
【问题讨论】:
标签: javascript typescript foreach promise lodash