与异步code碱基任何使用JavaScript的ES6类? [英] Are JavaScript ES6 Classes of any use with asynchronous code bases?

查看:264
本文介绍了与异步code碱基任何使用JavaScript的ES6类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么可以 ES6类提供,作为组织模式,以异步code。下面是ES7异步为例/ AWAIT,可以将ES6级在ES7异步方法或构造函数?

我可以这样做:

 类Foo {
    异步构造函数(){
        让解析度=等待getHTML();
        this.res = RES
    }
}

和,如果不是应如何构造的工作,做到这一点?

 类Foo {
    构造函数(){
        getHTML()。然后(功能(RES){
            this.res = RES
        }
    }
}

如果没有这些模式的工作,可以在ES6 构造函数(而且类)类支持任何形式的异步性的,关于对象的状态进行操作?或者说,只有他们是纯粹同步code碱基?以上的例子是在构造函数中,但他们并不需要是..下来推问题多了​​一个层次。

 类Foo {
    myMethod的(){
      / *我可以做任何事情,异步这里* /
    }
}

或者,用一个getter ...

 类Foo {
    获得myProp(){
        / *是否有任何情况下,这是有益的异步* /
    }
}

我能想到的是并行运行的东西同样的方法/构造函数/吸气内,但有整个事情的决心结束前的唯一例子。我只是困惑,因为它似乎与所有的推,完全异步的图书馆,这只是用来迷惑的事情。除了课本上的例子,我无法找到一个应用程序他们是很有用的。


解决方案

  

我可以做异步构造()


没有,这是一个语法错误 - 就像构造*()。构造函数是不返回任何东西(没有答应,没有发电机)的方法,它只是初始化实例。


  

和,如果不是应如何构造的工作,做到这一点。


这样的构造不应该存在,请参见它是不好的做法有一个构造函数返回一个承诺?


  

能否ES6类支持任何形式的不同步,关于对象的状态进行操作?或者说,是他们只为纯粹的同步code碱基?


是的,你可以使用异步方法(即使拟议异步语法)的类和getter方法​​可以返回承诺为好。

不过,你需要决定什么时候被调用的方法,而一些异步进程仍处于活动状态会发生什么。如果你想让它序列中的所有您的操作,你应该承诺里面存储您的实例的状态,该序列的结尾,你可以到链。或者,如果你想允许并行操作,最好的办法是让你的情况不变,并返回一个承诺的另一个实例。

What can ES6 Classes provide, as a pattern of organization, to asynchronous code. Below is an example with ES7 async/await, can an ES6-class have an asynchronous method, or constructor in ES7?

Can I do:

class Foo {
    async constructor() {
        let res = await getHTML();
        this.res = res
    }
}

And, if not how should a constructor work that does this?

class Foo {
    constructor() {
        getHTML().then( function (res) {
            this.res = res
        }
    }
}

If neither of these patterns work, can a constructor (and moreover classes) in an ES6 class support any form of asynchronicity that operates on the object's state? Or, are they only for purely synchronous code bases? The above examples are in the constructor, but they don't need to be.. Pushing the problem down one more level..

class Foo {
    myMethod () {
      /* Can I do anything async here */
    }
}

Or, with a getter...

class Foo {
    get myProp() {
        /* Is there any case that this is usefully asynchronous */
    }
}

The only examples I could think of is to run something in parallel inside of the same method/constructor/getter, but to have the whole thing resolve before conclusion. I'm just confused because it seems with all the push to fully asynchronous libraries, this just serves to confuse things. Except for textbook examples, I can't find one application they're useful for.

解决方案

Can I do async constructor()

No, that's a syntax error - just like constructor* (). A constructor is a method that doesn't return anything (no promise, no generator), it only initialises the instance.

And, if not how should a constructor work that does this

Such a constructor should not exist at all, see Is it bad practice to have a constructor function return a Promise?

Can ES6 classes support any form of asynchrony that operates on the object's state? Or, are they only for purely synchronous code bases?

Yes, you can use asynchronous methods (even with the proposed async syntax) on classes, and getters can return promises as well.

However, you will need to decide what should happen when a method is called while some asynchronous process is still active. If you want it to sequence all your operations, you should store your instance's state inside a promise for the end of that sequence that you can chain onto. Or, if you want to allow parallel operations, the best approach is to make your instances immutable and return a promise for another instance.

这篇关于与异步code碱基任何使用JavaScript的ES6类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆