【发布时间】:2020-10-05 16:58:42
【问题描述】:
我的小项目中有这段代码:
close(flush = false): void {
if (this.ws?.CLOSING || this.ws?.CLOSED) {
return;
}
if (flush) {
// I really don't know how to fix that
// eslint-disable-next-line @typescript-eslint/promise-function-async
const sendPromises = this.state.queue.map((message) =>
this.sendAsync(message)
);
void Promise.all(sendPromises).then(() => this.ws?.close());
} else {
this.ws?.close();
}
}
当我在其上运行 xo(使用 typescript-eslint)时,@typescript-eslint/promise-function-async 失败。我做了一些更改,但仍然失败。谁能给我解释一下为什么这不起作用?
我尝试了什么:
// first
const sendPromises: Promise<void> = this.state.queue.map((message) => this.sendAsync(message));
// second
const sendPromises = this.state.queue.map((message): Promise<void> => this.sendAsync(message));
【问题讨论】:
-
问题是
@typescript-eslint/promise-function-async还是@typescript-eslint/restrict-template-expressions? -
另外,规则可能希望你这样做
this.state.queue.map(async (message) => this.sendAsync(message) ); -
按照规则要求将返回 promise 的函数标记为异步?你读过github.com/typescript-eslint/typescript-eslint/blob/master/…吗?
标签: typescript eslint typescript-eslint