NestJS · Middle · Question 11 of 75

When would you use useFactory for a provider?

{
  provide: "REDIS",
  useFactory: async (config: ConfigService) => createClient({ url: config.get("REDIS_URL") }).connect(),
  inject: [ConfigService],
}

Explanation

Async factories delay app startup until they resolve, so the connection is ready before anything uses it.

Further reading

Problem with a question

NestJS · Middle · question 11

Test yourself

Take the NestJS · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz