Skip to content

Commit

Permalink
Merge pull request #116 from seasonedcc/await-trace
Browse files Browse the repository at this point in the history
feat: Add async capabilities for the trace function
  • Loading branch information
gustavoguichard authored Oct 24, 2023
2 parents 383b3ca + fd090e9 commit b00d471
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ traceToConsole(someOtherDomainFunction)()
It would also be simple to create a function that will send the errors to some error tracking service under certain conditions:

```ts
const trackErrors = trace(({ input, output, result }) => {
const trackErrors = trace(async ({ input, output, result }) => {
if(!result.success && someOtherConditions(result)) {
sendToExternalService({ input, output, result })
await sendToExternalService({ input, output, result })
}
})
```
Expand Down
8 changes: 6 additions & 2 deletions src/domain-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,15 @@ type TraceData<T> = {
* // ^? DomainFunction<number>
*/
function trace<D extends DomainFunction = DomainFunction<unknown>>(
traceFn: ({ input, environment, result }: TraceData<UnpackResult<D>>) => void,
traceFn: ({
input,
environment,
result,
}: TraceData<UnpackResult<D>>) => Promise<void> | void,
): <T>(fn: DomainFunction<T>) => DomainFunction<T> {
return (fn) => async (input, environment) => {
const result = await fn(input, environment)
traceFn({ input, environment, result } as TraceData<UnpackResult<D>>)
await traceFn({ input, environment, result } as TraceData<UnpackResult<D>>)
return result
}
}
Expand Down

0 comments on commit b00d471

Please sign in to comment.