RAII pattern: acquire a ref, run fn, auto-release.
fn
The ref is released in a finally block so cleanup is guaranteed even if fn throws. Zero chance of leaking.
finally
The service type.
The return type of fn.
The context to acquire from.
The service token.
Receives the unwrapped service value.
The return value of fn.
1.2.0
const balance = withRef(ctx, Tokens.RPC, rpc => rpc.getBalance(pubkey));// ref is auto-released — no leak Copy
const balance = withRef(ctx, Tokens.RPC, rpc => rpc.getBalance(pubkey));// ref is auto-released — no leak
RAII pattern: acquire a ref, run
fn, auto-release.The ref is released in a
finallyblock so cleanup is guaranteed even iffnthrows. Zero chance of leaking.