Slide 26
Slide 26 text
findOne(id: number): Effect.Effect {
return Effect.gen(this, function* () {
const [user, posts] = yield* Effect.all(
[
Effect.retry(
Effect.tryPromise(() => this.userApiService.getUserById({ userId: id })),
{
until: (error) => error instanceof ApiException && error.code === 404,
times: 3,
},
),
Effect.retry(
Effect.tryPromise(() => this.postApiService.getPosts({ userId: id, limit: 5 })),
{ times: 3 },
),
],
{ concurrency: 'inherit' },
);
return {
id: user.id,
username: user.username,
latestPosts: posts.map((post) => ({ id: post.id, title: post.title })),
};
}).pipe(
Effect.catchAll(({ error }) =>
error instanceof ApiException && error.code === 404
? Effect.fail(new NotFoundException('User not found', { cause: error }))
: Effect.die(error)
),
);
}
Future work: Effect with Generators in the whole codebase
26
26