Creating the Users Atom

Avatar of Hemanta SundarayHemanta Sundaray

Now that we have an Atom Runtime with access to UserService, we can create an Atom that fetches users’ data.

In atoms/user.ts, add the following code:

atoms/user.ts
import { Effect } from "effect";
import { atomRuntime } from "@/atom-runtime";
import { UserService } from "@/services/user-service";
// ============ Users Atom ============
export const usersAtom = atomRuntime.atom(
Effect.gen(function* () {
return yield* UserService.getUsers();
}),
);

We call atomRuntime.atom() and pass it an Effect. Inside the Effect, we call UserService.getUsers() to fetch users’ data.

Because usersAtom runs an Effect, it’s called an effectful Atom — a term we’ll use throughout the course to refer to any Atom that runs an Effect.

Sign in to save progress

Stay in the loop

Get notified when new chapters are added and when this course is complete.