我有一种方法,可以在从 SQLite
DB 获取数据后在我的 Ionic
应用程序中执行一些基本的数学计算。我想在页面上显示这些结果,但问题是我不确定如何将所有这些计算值放入对象数组中。
我尝试关注,但编辑提示没有解决 promise 。对我来说,我似乎已经通过从每个变量中提取数值并将它们分配给局部变量(例如grossMarketable、aphMarketable、amountSold 和totalContractDollarAmount )来解决它们。
home.ts
private calculate() {
console.log("**********Starting calculations now.....");
let calculations: CalcModel[] = [];
for (let i = 0; i < this.userCropTxModels.length; i++) {
let userCropTxModel = this.userCropTxModels[i];
console.log("userCropTxModel: " + userCropTxModel);
let grossMarketable = this.userCropProvider.getGrossMarketableByCropId(userCropTxModel.cropId)
.then(grossMarketable => {
console.log("grossMarketable: " + grossMarketable);
return grossMarketable;
})
.catch((e) => console.error(JSON.stringify(e)));
let aphMarketable = this.userCropProvider.getAPHMarketableByCropId(userCropTxModel.cropId)
.then(aphMarketable => {
console.log("aphMarketable: " + aphMarketable);
})
.catch((e) => console.error(JSON.stringify(e)));
let amountSold = this.userContractProvider.getTotalContractedBushelsByCropId(userCropTxModel.cropId)
.then(amountSold => {
console.log("amountSold: " + amountSold);
})
.catch((e) => console.error(JSON.stringify(e)));
let totalContractDollarAmount = this.userContractProvider.getTotalContractDollarAmountByCropId(userCropTxModel.cropId)
.then(totalContractDollarAmount => {
console.log("totalContractDollarAmount: " + totalContractDollarAmount);
})
.catch((e) => console.error(JSON.stringify(e)));
console.log("grossMarketable: " + grossMarketable);
console.log("aphMarketable: " + aphMarketable);
console.log("amountSold: " + amountSold);
console.log("totalContractDollarAmount: " + totalContractDollarAmount);
/**************************************************
//THE EDITOR IS SHOWING RED MARKS BELOW
***********************************************/
calculations.push({
cropName: 'Corn',
grossMarketable: grossMarketable,
grossMarketable: grossMarketable,
amountSold: amountSold,
totalContractDollarAmount: totalContractDollarAmount
});
}
console.log("calculations: " + calculations);
}
user-crop.ts(UserCropProvider的代码片段)
getGrossMarketableByCropId(cropId: number): Promise<number> {
return this.databaseProvider.getDatabase().then(database => {
return database.executeSql(SQL_SELECT_GROSS_MARKETABLE_BY_CROP_ID, [cropId])
.then((data) => {
let grossMarketable: number = 0;
for (let i = 0; i < data.rows.length; i++) {
grossMarketable = data.rows.item(i).GROSS_MARKETABLE
}
return grossMarketable;
});
});
}
CalcModel.ts
export interface CalcModel {
cropName: string;
grossMarketable: number;
aphMarketable: number;
amountSold: number;
totalContractDollarAmount: number;
}
最佳答案
为每个用户裁剪模型创建一个 Promise.all,其中包含异步请求的 Promise 列表。
当你解决内部的问题时,返回一个计算对象。 当您解决所有问题后,获取您的计算列表:
您的代码应类似于:
private calculate() {
const promises: Promise<any>[] = [];
for (let i = 0; i < this.userCropTxModels.length; i++) {
let userCropTxModel = this.userCropTxModels[i];
promises.push(Promise.all([
this.userCropProvider.getGrossMarketableByCropId(userCropTxModel.cropId),
this.userCropProvider.getAPHMarketableByCropId(userCropTxModel.cropId),
this.userContractProvider.getTotalContractedBushelsByCropId(userCropTxModel.cropId),
this.userContractProvider.getTotalContractDollarAmountByCropId(userCropTxModel.cropId)
]).then(data => ({
cropName: 'Corn',
grossMarketable: data[0],
amountSold: data[1],
totalContractDollarAmount: data[2]
})));
}
Promise.all(promises).then(calculations => console.log(calculations));
}
编辑
一些重构。我不知道它是否有效,我只是在编码,甚至没有尝试,但只是更干净一点:
private calculate() {
const promises: Promise<any>[] = this.userCropTxModels.map(userCropModel => Promise.all([
this.userCropProvider.getGrossMarketableByCropId(userCropModel.cropId),
this.userCropProvider.getAPHMarketableByCropId(userCropModel.cropId),
this.userContractProvider.getTotalContractedBushelsByCropId(userCropModel.cropId),
this.userContractProvider.getTotalContractDollarAmountByCropId(userCropModel.cropId)
]).then(data => ({
cropName: 'Corn',
grossMarketable: data[0],
amountSold: data[1],
totalContractDollarAmount: data[2]
})));
Promise.all(promises).then(calculations => console.log(calculations));
}
如果你想编写同步/样式代码,你甚至可以使用 async/await
关于javascript - 不确定如何在包含 Promises 的方法中填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54016961/