异步链接模式

考虑以下情况:


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


冉冉说
浏览 124回答 1
1回答

扬帆大鱼

警告await只能在async函数内部使用,您想要的那种 API 是可能的,只是稍微复杂一些。大量的库,例如knex,jQuery和nightmare.js实现链接以组成异步操作。但是可链接的方法不是异步的。相反,异步操作仅在操作结束时(当您想要结果时)执行,但方法本身是同步的。在的情况下knex,例如,异步操作仅执行时.then()被调用。这是您可以做到的一种方法:function Calc () {&nbsp; &nbsp; this.operations = [];&nbsp; &nbsp; this.value = 0;}Calc.prototype = {&nbsp; &nbsp; add: function (x) {&nbsp; &nbsp; &nbsp; &nbsp; // schedule a function that performs async addition:&nbsp; &nbsp; &nbsp; &nbsp; this.operations.push(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Promise(ok => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ok(this.value + x);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; },&nbsp; &nbsp; subtract: function (x) {&nbsp; &nbsp; &nbsp; &nbsp; // schedule a function that performs async subtraction:&nbsp; &nbsp; &nbsp; &nbsp; this.operations.push(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Promise(ok => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ok(this.value - x);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; },&nbsp; &nbsp; // THIS IS WHERE WE DO OUR MAGIC&nbsp; &nbsp; then: async function (callback) {&nbsp; &nbsp; &nbsp; &nbsp; // This is finally where we can execute all our&nbsp; &nbsp; &nbsp; &nbsp; // scheduled async operations:&nbsp; &nbsp; &nbsp; &nbsp; this.value = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (let i=0; i<this.operations.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.value = await operations[i]();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return callback(this.value); // since the await keyword will call our&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// then method it is the responsibility of&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// this method to return the value.&nbsp; &nbsp; }}现在你可以像这样使用它:async function main () {&nbsp; &nbsp; let x = await new Calc().add(2).subtract(1);&nbsp; &nbsp; console.log(x);}main();请注意,上面的代码在功能上等效于:new Calc().add(2).subtract(1).then(x => console.log(x));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript