【发布时间】:2019-05-26 22:55:16
【问题描述】:
我想要返回以下函数 Promise
async fetchCommentLines(commitDict: CommitDict): Promise < number[] > {
if (GitCommentService.isLoggedIn()) {
const commentLines = Object.values(commitDict).map(async commit => {
// ...
// do the job and return number[]
// ...
return lineNums;
});
Promise.all(commentLines)
.then(commitLines => {
return Array.prototype.concat.apply([], commitLines);
});
} else {
return [] as number[];
}
}
首先我得到“函数缺少结束返回语句并且返回类型不包括'undefined'”
然后我添加了 undefined (所以返回类型变为 Promise)
但这次我得到“并非所有代码路径都返回值”。
看来我没有考虑下面代码的可能代码路径
Promise.all(...)
.then(val => {return ...})
我错过了什么?
我也试过了
Promise.all(...)
.then(val => {return ...})
.catch(e => {return ...})
但是没用
注意:我的主要目的是返回Promise,而不是Promise
【问题讨论】:
标签: javascript typescript promise async-await es6-promise