【发布时间】:2021-10-29 14:27:55
【问题描述】:
我试图理解为什么以下方法中的返回类型string 带有红色下划线作为错误:
exportPageAsText(pageNumber: number): string {
(async () => {
const text = await this.pdfViewerService.getPageAsText(pageNumber);
console.log(text);
return text;
})();
}
错误消息显示:A function whose declared type is neither 'void' nor 'any' must return a value. 所以我将return text; 移出async 范围并将其放在})(); 之后,但这使得text 变量无法识别。
然后我想可能是因为方法返回类型应该是Promise所以我将签名更改为:
exportPageAsText(pageNumber: number): Promise<string>
但我收到一个新错误,提示 A function whose declared type is neither 'void' nor 'any' must return a value.
有人可以帮我理解我做错了什么吗?
【问题讨论】:
-
我认为当你这样做时:
(async () => {,你正在创建一个新函数,所以这个函数应该有一个返回类型 -
你没有返回任何东西,只是执行箭头异步函数。只需在 (async()=>...) 之前添加 return。
标签: javascript typescript asynchronous async-await