解决将变量更改为一定值的承诺

时间:2018-12-28 13:14:16

标签: javascript typescript promise reactivex

我正在使用reactivextypescript编写一个控制充电电池的应用程序。我在这里使用了很多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

谢谢!

0 个答案:

没有答案