我正在使用reactivex
和typescript
编写一个控制充电电池的应用程序。我在这里使用了很多promise,这使我想到了一个问题:我希望链中的promise只要变量(收费状态)达到一定值就可以解决。
我尝试使用while
循环来定期检查该值,并在达到该值后立即返回,但这会阻塞整个程序。
private _performChargeJob(job: ChargeJob): Promise<any> {
// Return a promise that is resolved if the job has finished processing.
return this._performGoal(job.goals[0].goalId)
.then(() => {
this._handleCharge(this._soc, 100); // this is the important part where I'd wish the promise to resolve
}).then(() => {
this.logConsole("Finished charging.");
this.callRosService({name: "set_charge_state", serviceType: "insystems_srvs/SetChargeState"}, {
set_charge_state: 0, // stop charging
});
});
}
private _handleCharge(soc: number, maxSoc: number) {
// this function should return/resolve a promise when variable soc reaches 100.0
this.logConsole("starting charge.");
this.callRosService({name: "set_charge_state", serviceType: "insystems_srvs/SetChargeState"}, {
set_charge_state: 1, // start charging
});
}
在这里,我期望某种watch()
(已过时)或新的promise
可以等待该值达到该水平。甚至可能是observable
?
谢谢!