考虑以下情况:
var Calc = function () {
// proprties
this.value = 0
// handler's
this.Add = (__value) => { this.value = this.value + __value; return this }
this.Subtract = (__value) => { this.value = this.value - __value; return this }
}
var = (new Calc()).Add(2).Subtract(1) // console.log() => 1
但是如果你在 async 中包装 Object 等待类似的东西
var Calc = async function () {
// proprties
this.value = 0
// handler's
this.Add = async (__value) => { this.value = this.value + __value; return this }
this.Subtract = async (__value) => { this.value = this.value - __value; return this }
}
(await new Calc()).Add(2).Subtract(1) // console.log() => undefined
(await (await new Calc()).Add(2)).Subtract(1) // console.log() => 1
我知道返回 Promise 的原因需要解决,因为您只需将代码包装在 () 中,一旦执行语句,您就可以继续链。
我在寻找什么。
await newCalc().Add(2).Subtract(1) // console.log() => 1
扬帆大鱼
相关分类