Caching Effect

Hi, how can I make the getIdEffect to only run one time and the cache any extra runs.

The idea is to make getIdEffect called even in nested effects.

import { NextResponse } from "next/server"
import { Effect } from "effect"

const getId = async (id: string) => {
  await new Promise((resolve) => setTimeout(resolve, 1000))
  return id
}

const getIdEffect = (id: string) =>
  Effect.promise(() => getId(id)).pipe(Effect.withRequestCaching(true))

export const GET = async () => {
  const effect = Effect.gen(function* () {
    yield* getIdEffect("123")
    yield* getIdEffect("123")
    return yield* getIdEffect("123")
  })

  const response = await Effect.runPromise(effect)

  return NextResponse.json(response)
}
Was this page helpful?