简体   繁体   English

调用接口指定的方法时可以使用async / await吗?

[英]Is it possible to use async/await when invoking a method specified by an inteface?

Given: 鉴于:

    export interface UserAuthenticator
    {
        async isValid(username: string, password: string): Promise<boolean>
    }

Compiling generates error message TS1070: 'async' modifier cannot appear on a type member. 编译会生成错误消息TS1070: 'async' modifier cannot appear on a type member.

The async keyword is used on the implementation of the method. 在该方法的实现中使用async关键字。 It doesn't have any meaning on the interface itself. 它在接口本身上没有任何意义。 One implementation of the interface could use async/await in its implementation, and another could just return a Promise without the use of await. 接口的一种实现可以在其实现中使用async / await,另一种可以仅返回Promise而不使用await。 Theoretically both styles of implementation could have the same behavior and would both conform to the interface. 从理论上讲,两种实现方式都可以具有相同的行为,并且都符合接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM