首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >S6和谐异步类方法链

S6和谐异步类方法链
EN

Stack Overflow用户
提问于 2018-05-17 10:50:52
回答 2查看 624关注 0票数 0

这是一个简单的JavaScript示例:

代码语言:javascript
复制
class Test {
    constructor() {
        this.a = 0;
        this.b = 1;
    }

    doSomething = async () => {
        await this.doSomethingChained(1)
            .doSomethingChained(2)
            .doSomethingChained(3);
    };

    doSomethingChained = async (x) => {
        this.a = this.a + x;
        console.log('a is', this.a);
        return this;
    };
}

然后开始使用测试方法,但这并不重要。

代码语言:javascript
复制
test('Vorgang erfassen', async t => {

    const t1 = new Test();
    await t1.doSomething();

控制台包含以下内容:

代码语言:javascript
复制
a is 1
TypeError: _this2.doSomethingChained(...).doSomethingChained is not a function

我不明白为什么this.a工作,但return this不工作。当然,我可以一个接一个地开始这个方法,但是我很想用链子。

代码语言:javascript
复制
doSomething = async () => {
    await this.doSomethingChained(1);
    await this.doSomethingChained(2);
    await this.doSomethingChained(3);
};

就像一种魅力。

代码语言:javascript
复制
a is 1
a is 3
a is 6
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-17 11:14:11

根据此语法进行的链接:

代码语言:javascript
复制
this.doSomethingChained(1).doSomethingChained(2)

..。按定义是同步的: JavaScript将计算参数和函数同步调用;对此您无能为力。所以这个语法不适合异步代码。

异步代码本质上依赖于回调。甚至承诺都涉及回调的异步执行。对于await,这是不可见的,但是相应的async函数的执行上下文会异步恢复,在解析await之后继续执行。

关于你的问题:

我不明白为什么this.a工作,但return不工作。

return this确实可以工作,但是根据规范,return语句在async函数中提供的值定义了承诺值,而不是doSomethingChained的返回值,因为async函数总是返回承诺(同步)。

因此,在您的代码中,(同步!)doSomethingChained的返回值是一个承诺,而不是this。这就解释了为什么你会犯这个错误。

注意,当async函数没有await时,它不是很有用。没有它,你也可以使用一个正常的函数。

票数 1
EN

Stack Overflow用户

发布于 2019-06-22 23:48:19

我认为你可以通过使用Promise.all来连锁承诺

代码语言:javascript
复制
         class Laundry {

          constructor() {
            this.queue = [];
            this.clothes = [];
          }


          addClothes(cloth) {
            this.clothes.push(cloth);

            return this;
          }


          removeClothes(cloth) {
            const clothIndex = this.clothes.findIndex(value => value === cloth);
            this.clothes.splice(clothIndex, 1);

            return this;
          }


          wash() {
            this.clothes = 'washed';

            const item = new Promise(resolve => setTimeout(() => resolve(1), 3000)).catch()

            this.queue.push(item);

            return this;
          }


          rinse() {
            this.clothes = 'rinsed';

            const item = new Promise(resolve => setTimeout(() => resolve(2), 2000)).catch()

            this.queue.push(item);

            return this;
          }


          finish() {
            return Promise.all(this.queue);
          }
        }


        module.exports = Laundry;

然后这样打电话..。

代码语言:javascript
复制
const Laundry = require('./laundry');

const laundry = new Laundry();

// class chain methods...
laundry.wash().rinse().finish().then(console.log).catch(console.error);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50389606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档