By using "use server"
the function will be executed on the server. This means that we can use RuntimeServer
to execute effects just like we did with server components:
src/actions/like-post.ts
"use server";
import { Effect } from "effect";
import { RuntimeServer } from "../services/RuntimeServer";
export const likePost = (id: number) =>
RuntimeServer.runPromise(
/// Effect here
);
You can now execute any server logic inside likePost
.
We are not going to add other services for this example, but you can imagine creating a
Database
service that executes a query to add a like to a post.
In this example we simulate a delay using Effect.sleep
:
src/actions/like-post.ts
"use server";
import { Effect } from "effect";
import { RuntimeServer } from "../services/RuntimeServer";
export const likePost = (id: number) =>
RuntimeServer.runPromise(
Effect.sleep("2 seconds"),
);