在创建价格的过程中,我需要从服务器获取有关影响最终价格的选定选项的数据。
所以我用一种计算方法创建了一个类Price
:
class Price {
constructor () {
this.totalUnitPrice = 0
};
_updateTotal () {
// apply lang rate
this.totalUnitPrice = 0;
this.totalUnitPrice += +this.basePrice.value + +this.langRate.textContent * +this.basePrice.value / 100;
// apply price unit lot
this.totalUnitPrice = this.totalUnitPrice * this._getQuantityLength();
// apply options
for (var i = 0; i < this.options.length ; i++) {
if (this.options[i].selected) {
this._get_option_value(this.options[i].value);
}
}
// apply period
this.totalUnitPrice += this.period * this.totalUnitPrice / 100;
let total = this.totalUnitPrice * +this.quantity.value;
// display
this.dTotalUnit.textContent = this.totalUnitPrice.toFixed(2) + ' €';
this.dTotal.textContent = total.toFixed(2) + ' €';
}
// At this time, ```this.totalUnitPrice``` is not updated
_get_option_value (opk) {
var that = this;
aGet({"option": opk}).then(
function (data) {
if (data.is_percent === true) {
that.totalUnitPrice += that.totalUnitPrice * parseInt(data.price) / 100
} else {
that.totalUnitPrice += data.price
}
console.log(data.price);
return data;
},
);
}
}
和一个用于获取请求的小函数:
async function aGet(args) {
return await $.get("", args);
}
这里的问题是,除了未在自己的范围之外更新价格的选项之外,价格计算正确。我以为使用await / async可以解决我的问题。我错了。那怎么可能实现我的目标?
答案 0 :(得分:0)
要超出承诺的范围,您需要对override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var destination = segue.destination as! SecondViewController
destination.myString = outputName.text!
}
函数使用async / await语法
_get_option_value