Files
medusa-store/www/apps/book/app/advanced-development/modules/service-constraints/page.mdx
Shahed Nasser fb67d90b64 docs: improvements + additions to module docs (#9152)
- Split Module and Module Links to their own chapters
- Add new docs on db operations and transactions in modules, multiple services, links with custom columns, etc...
- Added a list of registered dependencies in a module container
2024-10-01 11:20:54 +00:00

41 lines
1.1 KiB
Plaintext

export const metadata = {
title: `${pageNumber} Service Constraints`,
}
# {metadata.title}
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 asynchronus, it always executes methods in the wrapper with the `await` keyword.
For example, if you have a synchronous `getMessage` method, and you use it other resources like workflows, Medusa executes it as an async method:
```ts
await helloModuleService.getMessage()
```
So, make sure your service's methods are always async to avoid unexpected errors or behavior.
```ts highlights={[["8", "", "Method must be async."], ["13", "async", "Correct way of defining the method."]]}
import { MedusaService } from "@medusajs/framework/utils"
import MyCustom from "./models/my-custom"
class HelloModuleService extends MedusaService({
MyCustom,
}){
// Don't
getMessage(): string {
return "Hello, World!"
}
// Do
async getMessage(): Promise<string> {
return "Hello, World!"
}
}
export default HelloModuleService
```