这是用 TypeScript 编写的 vue-router
的包装器:
abstract class RoutingHelper {
// ...
public static redirectToNamedRoute(targetRouteName: AnyValueOf<typeof NamedRoutes>): Promise<void> {
return new Promise<void>((resolve: () => void): void => {
router.push({ name: targetRouteName }, resolve);
});
}
}
规则@typescript-eslint/promise-function-async强制返回 Promise
的方法将是 async
:
public static async redirectToNamedRoute(targetRouteName: AnyValueOf<typeof NamedRoutes>): Promise<void> {
return new Promise<void>((resolve: () => void): void => {
router.push({ name: targetRouteName }, resolve);
});
}
现在require-await告诉我该方法必须包含 await
关键字。我如何在此方法中观察 require-await
?
最佳答案
return await new Promise<...>(...);
会让 linter 高兴。但老实说,在这里执行这条规则是没有意义的。如果您想在返回之前处理 promise 结果,则等待 promise 才有意义。
关于javascript - ES6 : How to use await with callbacks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57883724/