繁体   English   中英

async / await在一个名为“then”的类方法中

[英]async/await in a class method called “then”

我创建了一个具有“then”方法的类。 这个类与Promise类型无关; “then”方法有不同的目的,不会返回承诺。 我试图在Typescript 2.1.4中编写async / await函数,等待并返回此类的实例,但VS Code中的Typescript服务器给了我错误。 如果我将方法重命名为“then”以外的其他方法,则错误消失。

带错误的示例代码:

class MyClass {
    then(): number {
        // this method isn't related to Promise.then
        return 2 + 2;
    }
}

// three errors below go away when "then" is renamed

// [ts] An async function or method must have a valid awaitable return type.
async function my_async(): Promise<MyClass> {
    let a: Promise<MyClass> = Promise.resolve(new MyClass());

    // [ts] Operand for 'await' does not have a valid callable 'then' member.
    let b: MyClass = await a;

    // [ts] Return expression in async function does not have a valid callable 'then' member.
    return b;
}

有人可以解释为什么不允许使用具有自己的“then”方法的对象的promises,或者解决这个问题?

Promise被定义为具有名为.then的方法的对象。 例如,它们没有被定义为“由require('bluebird/promise')返回的模块”。

使这一点变得重要的部分原因是,当一个promise库正在解析一个promise时,如果该结果是一个promise本身,那么它并不意味着在.then调用中提供最终结果。 例如:

function myFn() {
  // doPromise1 will eventually give us `1`.
  return doPromise1().then(x => {
    // doPromise2 will eventually give us `2`.
    return doPromise2();
  });
}

调用此函数和调用.then对结果不会返回我的内部承诺doPromise2() 它将返回2 - 所以它将等到两个promises完成,并给出最终值。

这是,如果我回来了不同的3then 它会看到结果不是承诺,并将其作为最终值提供。 然而,问题的关键在于它如何知道这不是一个承诺。 它没有做类型检查,就像if p instanceof Promise因为在各种库中有太多的Promise定义和polyfill,并且它们意味着一起工作。 相反,他们检查一些通用的东西应该是这样的: if (typeof p.then === 'function')

对于您的班级,该检查将返回true ,这会使其认为您的值本身就是Promise。 它运行它,希望得到另一个promise对象,但得到一个数字并失败。

then方法关系到无极API -但从JavaScript的鸭子类型来看,你的类是thenable看到规范 ,特别是这一步

...
Let then be Get(resolution, "then").
...

如果你的诺言解析为具有then函数的对象,那么它具有特殊的意义 - 它是一个thenable 您不能生成具有then函数的结果对象,也不能通过Promise解析算法处理它们。

暂无
暂无

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

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