Service Constraints

This chapter lists constraints to keep in mind when creating a service.

Use Async Methods#

Medusa wraps service method executions to inject useful context or transactions. However, since Medusa can't detect whether the method is asynchronous, it always executes methods in the wrapper with the await keyword.

For example, if you have a synchronous getMessage method, and you use it in other resources like workflows, Medusa executes it as an async method:

Code
await helloModuleService.getMessage()

So, make sure your service's methods are always async to avoid unexpected errors or behavior.

Code
1import { MedusaService } from "@medusajs/framework/utils"2import MyCustom from "./models/my-custom"3
4class HelloModuleService extends MedusaService({5  MyCustom,6}){7  // Don't8  getMessage(): string {9    return "Hello, World!"10  }11
12  // Do13  async getMessage(): Promise<string> {14    return "Hello, World!"15  }16}17
18export default HelloModuleService
Was this chapter helpful?
Edit this page
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break