effect-io-ai

Package: effect
Module: Ref

Ref.setAndGet

Sets the value of the Ref atomically to the specified value and returns the new value.

When to use

Use when you want to set a Ref value and immediately get it back in one atomic operation.

Example (Setting and returning the new value)

import { Effect, Ref } from "effect"

const program = Effect.gen(function*() {
  const ref = yield* Ref.make(10)

  // Set new value and get it back in one operation
  const newValue = yield* Ref.setAndGet(ref, 42)
  console.log(newValue) // 42

  // Verify the ref contains the new value
  const current = yield* Ref.get(ref)
  console.log(current) // 42
})

// Useful for sequential operations
const program2 = Effect.gen(function*() {
  const counter = yield* Ref.make(0)

  const newValue = yield* Ref.setAndGet(counter, 20)
  console.log(newValue) // 20
})

Signature

declare const setAndGet: (<A>(value: A) => (self: Ref<A>) => Effect.Effect<A>) & (<A>(self: Ref<A>, value: A) => Effect.Effect<A>)

Source

Since v2.0.0