Package: effect
Module: Ref
Updates the value of the Ref atomically using the given function and returns the new value.
When to use
Use to apply a Ref state transition and return the new stored value.
Example (Updating and returning the new value)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
const counter = yield* Ref.make(5)
// Update and get the new value in one operation
const newValue = yield* Ref.updateAndGet(counter, (n) => n * 3)
console.log(newValue) // 15
// Verify the ref contains the new value
const current = yield* Ref.get(counter)
console.log(current) // 15
})
See
update for updating without returning the new valuegetAndUpdate for returning the previous value insteadSignature
declare const updateAndGet: (<A>(f: (a: A) => A) => (self: Ref<A>) => Effect.Effect<A>) & (<A>(self: Ref<A>, f: (a: A) => A) => Effect.Effect<A>)
Since v2.0.0