Comparing Effect-based and traditional error handling approaches in TypeScript

hi, both code does the same thing, so what benefits i get doing with effect, since with effect i have to write extra. at the end, i have to throw the errors in this case.

await Effect.forEach(
  options.guards || [ctx.auth.defaultGuard],
  (guard) =>
    new NotGuestUserException().pipe(
      Effect.whenEffect(
        Effect.tryPromise({
          try: async () => await ctx.auth.use(guard).check(),
          catch: (err) => new InternalServerException({ error: err }, 'Unexpected error while checking the authentication status'),
        })
      )
    ),
  { discard: true }
).pipe(executePromiseEffect)

// without effect
for (let guard of options.guards || [ctx.auth.defaultGuard]) {
  if (await ctx.auth.use(guard).check()) {
    throw new NotGuestUserException()
  }
}
Was this page helpful?