Skip to content

Is there a better way to call two async functions in request preHandler hook? #4595

@varunkamra

Description

@varunkamra

Assume that I have two classes A and B and each class has one async function each.

export class A {
   public static async handle(request, reply) {
     // content
   }
}

export class B {
  public static async handle(request, reply) {
     // content
   }
}

I want to call both of these functions in the preHandler hook of a request. Currently I am doing something like this:

preHandler: async (request, reply) => {
   await A.handle(request, reply);
   await B.handle(request, reply);
}

💬 Question

Is this the best way to call two async functions in the preHandler or is there a better cleaner way of calling these two functions in the preHandler? I checked the preHandler definition and that it only accepts either an array of synchronous functions that can be called or a single async function as it's value, is that right?

Your Environment

  • node version: 18
  • fastify version: >=4.11.0
  • os: Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions