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* () {
const userService = yield* UserService;
return yield* userService.getUsers();
}),
);

We call atomRuntime.atom() and pass it an Effect. Inside the Effect, we yield the UserService to get the service instance, then call getUsers() on it 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 Effect Atom related content is published.