Search code examples
javascripttypescriptecmascript-6async-awaites6-promise

ES6: How to use await with callbacks


Here is the wrapper for vue-router written on TypeScript:

abstract class RoutingHelper {

  // ...

  public static redirectToNamedRoute(targetRouteName: AnyValueOf<typeof NamedRoutes>): Promise<void> {
    return new Promise<void>((resolve: () => void): void => {
      router.push({ name: targetRouteName }, resolve);
    });
  }
}

The rule @typescript-eslint/promise-function-async forces that method which returns Promise will be async:

public static async redirectToNamedRoute(targetRouteName: AnyValueOf<typeof NamedRoutes>): Promise<void> {
    return new Promise<void>((resolve: () => void): void => {
      router.push({ name: targetRouteName }, resolve);
    });
  }

Now require-await tells me that method must include await keyword. How I can observe require-await in this method?


Solution

  •  return await new Promise<...>(...);
    

    will make the linter happy. But honestly, there is no sense in enforcing that rule here. It only makes sense to await a promise if you want to work with the promises result before returning.