www/apps/book/app/learn/fundamentals/modules/service-constraints/page.mdx
export const metadata = {
title: ${pageNumber} Service Constraints,
}
This chapter lists constraints to keep in mind when creating a service.
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:
await blogModuleService.getMessage()
So, make sure your service's methods are always async to avoid unexpected errors or behavior.
import { MedusaService } from "@medusajs/framework/utils"
import Post from "./models/post"
class BlogModuleService extends MedusaService({
Post,
}){
// Don't
getMessage(): string {
return "Hello, World!"
}
// Do
async getMessage(): Promise<string> {
return "Hello, World!"
}
}
export default BlogModuleService