Ramda.js R.once() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

R.once(fn) returns a wrapper that runs fn only the first time you call it. Later calls return that first result and skip fn entirely — even if the arguments change.

Stable: R.once has been in Ramda since v0.1.0. It is not deprecated, experimental, or non-standard. For per-argument caching, use R.memoizeWith instead.

Syntax

Call R.once. Start with this line.

Ramda
R.once(fn)
CallWhat it does
R.once(fn)Primary signature.
R.once(...)Call the function helper.

Prefer import { once } from "ramda" when you want a named import.

Library declarations
Ramda
R.once(fn)

Parameters

ParameterDescription
fnRequired. Function to guard (runs at most once)

Return Value

Return ValueDescription
*Wrapper with the same call shape as fn

Ramda Examples

Example 1 — Official addOneOnce

Add one on the first call. The nested second line still returns that first 11.

Ramda
const addOneOnce = R.once(x => x + 1);

console.log(addOneOnce(10));
// → 11

console.log(addOneOnce(addOneOnce(50)));
// → 11
Try It Yourself

How It Works

  1. Wrap. R.once builds a latch around x => x + 1.
  2. First call. addOneOnce(10) runs the body and stores 11.
  3. Inner later call. addOneOnce(50) does not compute 51 — it returns 11.
  4. Outer later call. Still latched, so the whole expression is 11.

Example 2 — Later Arguments Do Not Matter

Greet Ada first. Grace never gets a new string.

Ramda
const greetOnce = R.once(function (name) {
  return 'Hi ' + name;
});

console.log(greetOnce('Ada'));
// → "Hi Ada"

console.log(greetOnce('Grace'));
// → "Hi Ada"
Try It Yourself

How It Works

  1. First name. 'Ada' builds and stores "Hi Ada".
  2. Second name. 'Grace' is ignored.
  3. Same string. The wrapper only returns the stored greeting.
  4. When wrong. If each name needs its own result, use memoizeWith or a plain function.

Example 3 — Side Effects Run Once

The function body bumps a counter. Three wrapper calls still leave the counter at 1.

Ramda
let n = 0;
const bumpOnce = R.once(function () {
  n += 1;
  return n;
});

console.log(bumpOnce());
console.log(bumpOnce());
console.log(bumpOnce());
console.log('n=' + n);
// → 1, 1, 1, n=1
Try It Yourself

How It Works

  1. First call. Body runs: n becomes 1, result stored as 1.
  2. Later calls. Body is skipped — no more bumps.
  3. Return. Each call still reports the stored 1.
  4. Rule. once skips the whole function, not only the return statement.

Example 4 — once vs memoizeWith

once freezes on 11. memoizeWith still computes 51 for a new argument.

Ramda
const addOnce = R.once(R.add(1));
const addMemo = R.memoizeWith(String, R.add(1));

console.log(addOnce(10));
console.log(addOnce(50));
// → 11, 11

console.log(addMemo(10));
console.log(addMemo(50));
// → 11, 51
Try It Yourself

How It Works

  1. once. One slot — after 10 → 11, 50 still yields 11.
  2. memoizeWith. Keys on '10' and '50', so both additions run.
  3. Pick once when the first answer should win for the wrapper’s lifetime.
  4. Pick memoize when different args should keep different results.

Example 5 — Same Object, Two Independent Wrappers

Later calls return the identical object. A second R.once wrapper has its own latch.

Ramda
const initA = R.once(function () {
  return { n: 1 };
});
const initB = R.once(function () {
  return { n: 2 };
});

console.log(initA() === initA());
// → true

console.log(initB().n);
// → 2
Try It Yourself

How It Works

  1. Singleton. initA() creates one object and reuses it.
  2. Same reference. initA() === initA() is true.
  3. Separate latch. initB does not share state with initA.
  4. Trap. Do not write R.once(boot)() at every call site — that boots again every time.

Browser & Runtime Support

R.once is a Ramda Function helper since v0.1.0. It is plain JavaScript — no special browser API. It runs anywhere Ramda loads (CDN, npm, bundler).

✓ Stable · Ramda 0.1.0+

R.once()

Available in every Ramda full build since the earliest releases. Works in evergreen browsers and current Node LTS / Bun / Deno when Ramda is installed.

100% With Ramda 0.1+
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.once Ramda 0.1.0+

Bottom line: Safe for production once Ramda is loaded. Use a CDN build such as ramda.min.js in the browser, or npm install ramda for Node. No polyfill is needed beyond shipping Ramda itself.

Frequently Asked Questions

R.once(fn) returns a new function. The first time you call it, it runs fn and remembers the return value. Every later call returns that same value and does not run fn again, even if the arguments change. Added in v0.1.0.
addOneOnce = R.once(x => x + 1); addOneOnce(10) //=> 11. Then addOneOnce(addOneOnce(50)) //=> 11 because the first result is already stored and later arguments are ignored.
once stores one result for the whole wrapper, no matter which arguments arrive later. memoizeWith stores a result per cache key, so different arguments can still compute different values.
No. After the first successful call, arguments are ignored. addOneOnce(10) is 11; addOneOnce(50) is still 11.
No. Each call to R.once(fn) builds a fresh wrapper with its own called flag and result. Reuse the function once returned. Calling R.once(fn) again starts a new first run.
No. R.once is a stable Ramda Function helper from v0.1.0. It is not deprecated, experimental, or non-standard.

About the author

Next: R.otherwise()

Attach a recovery function when a promise rejects — Ramda’s catch helper.

R.otherwise() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful