【问题标题】:The return type of an async function or method must be the global Promise<T> type异步函数或方法的返回类型必须是全局 Promise<T> 类型
【发布时间】:2019-06-23 15:10:04
【问题描述】:

我正在使用带有异步的匿名函数,并且在编译时发生错误。我在父函数中调用这个函数作为等待。

我有两个功能:

  1. 父函数
  2. 子函数

在父函数中,我使用的是异步等待。并调用第二个函数(即箭头函数)。如果我将该函数写为async function functionName (parameters) {},它可以工作,但我将它用作export const functionName = async (params) =&gt; {},它给了我这个错误。

const a = await b(data);

const b = async (data) => { return "success" }

我尝试添加const functionName = async (params) Promise&lt;string&gt; =&gt; {},但还是同样的错误。

在父函数中,我将子函数称为,

如何避免这个错误。

【问题讨论】:

    标签: typescript ecmascript-6 promise async-await


    【解决方案1】:

    定义方法的返回类型时需要添加: 您还需要指定承诺返回的类型

    const functionName = async (params): Promise<any> => {} 
    

    【讨论】:

    • 我也这样做了 const functionName = async (params): Promise => {} 仍然发生错误。
    • @AmitChauhan 是的。如果你定义了那个函数但是像{}这样保持body为空,你会得到错误,因为你定义了这个函数将返回promise但你没有返回任何东西
    • 那只是为了让你明白我们可以在那里返回 true。
    猜你喜欢
    • 1970-01-01
    • 2019-07-11
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2017-02-18
    • 1970-01-01
    • 2015-05-16
    相关资源
    最近更新 更多