Ramda.js R.thunkify() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

A helper that turns a function into a thunk maker: you give it the arguments now, and it hands back a zero-argument function (a thunk) that runs the real call later. The saved arguments live in a closure.

Status: added in Ramda v0.26.0 — stable, not deprecated, experimental, or non-standard.

Syntax

Call R.thunkify. Start with this line.

Ramda
R.thunkify(fn)(…args)()
CallWhat it does
R.thunkify(fn)(…args)()Primary signature.
R.thunkify(...)Call the function helper.

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

Library declarations
Ramda
R.thunkify(fn)(…args)()

Parameters

This helper has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*The value returned by R.thunkify().

Ramda Examples

Example 1 — Official Samples

Save the arguments, then run with the final ().

Ramda
console.log(R.thunkify(R.identity)(42)());
// → 42

console.log(R.thunkify((a, b) => a + b)(25, 17)());
// → 42
Try It Yourself

How It Works

  1. Wrap. R.thunkify(R.identity) waits for one argument.
  2. Save. (42) stores 42 and returns a thunk.
  3. Run. The last () calls R.identity(42) → 42.
  4. Two args. Same idea: (25, 17) saves both, () adds them → 42.

Example 2 — Supply Arguments One at a Time

The wrapper is curried, so you can pass arguments in separate calls.

Ramda
const add = (a, b) => a + b;

const add25 = R.thunkify(add)(25);
console.log(typeof add25);
// → "function" (still waiting for b)

const thunk = add25(17);
console.log(thunk());
// → 42
Try It Yourself

How It Works

  1. Arity. add.length is 2, so the wrapper waits for two arguments.
  2. Partial. (25) gives only one, so add25 still waits for b.
  3. Complete. add25(17) supplies the last argument → a thunk.
  4. Run. thunk() calls add(25, 17) → 42.

Example 3 — Delay a Side Effect

Prepare a call now; nothing happens until the thunk runs.

Ramda
const log = [];
const record = msg => {
  log.push(msg);
  return msg.length;
};

const later = R.thunkify(record)("saved");
console.log(log);
// → []

console.log(later());
// → 5

console.log(log);
// → ["saved"]
Try It Yourself

How It Works

  1. Prepare. later holds "saved", but record hasn’t run.
  2. Check. log is still empty — no side effect yet.
  3. Run. later() calls record("saved"), which pushes and returns 5.
  4. After. log now holds ["saved"].

Example 4 — Run Every Time vs R.once

A thunk doesn’t cache. Wrap it with R.once when the work should happen only once.

Ramda
let calls = 0;
const greet = name => {
  calls += 1;
  return "Hi " + name;
};

const thunk = R.thunkify(greet)("Ada");
thunk();
thunk();
console.log(calls);
// → 2

const onceThunk = R.once(R.thunkify(greet)("Bo"));
onceThunk();
console.log(onceThunk());
// → "Hi Bo"

console.log(calls);
// → 3
Try It Yourself

How It Works

  1. No cache. Calling thunk() twice runs greet twice → calls is 2.
  2. Once. R.once runs the thunk the first time and remembers the result.
  3. Reuse. The second onceThunk() returns the stored "Hi Bo" without running greet.
  4. Total. Only one extra run → calls is 3.

Example 5 — Build a Task List, Run It Later

Thunks are plain values, so you can store them in an array and run them all at once.

Ramda
const add = (a, b) => a + b;

const tasks = [
  R.thunkify(add)(1, 2),
  R.thunkify(add)(10, 20)
];

console.log(R.map(R.call, tasks));
// → [3, 30]
Try It Yourself

How It Works

  1. Queue. Each array item is a thunk; no addition has happened yet.
  2. Run all. R.map visits each thunk.
  3. Call. R.call(task) simply runs task().
  4. Collect. The results 3 and 30 form a new array.

Browser & Runtime Support

R.thunkify was added in Ramda v0.26.0. Ramda’s browser build is plain ES5, so it runs in any modern browser and in Node.js / Bun / Deno with npm install ramda.

✓ Stable · Ramda 0.26.0+

R.thunkify()

Check your Ramda version first — this helper is missing before 0.26.0. The examples use ES2015 arrow functions and let, supported by every evergreen browser.

100% With Ramda 0.26.0+
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.thunkify Ramda 0.26.0+

Bottom line: Safe for production with Ramda 0.26.0 or newer. On older builds R.thunkify is undefined; write () => fn(a, b) by hand instead. The labs on this page use [email protected].

Frequently Asked Questions

R.thunkify(fn) lets you give fn its arguments now and run it later. R.thunkify(fn)(a, b) saves the arguments and returns a thunk — a function with no parameters. Calling thunk() runs fn(a, b). The docs sample: R.thunkify((a, b) => a + b)(25, 17)() //=> 42.
R.partial(fn, [a])(b) runs fn as soon as the last argument arrives. R.thunkify(fn)(a, b) does not run fn yet — it returns a thunk, and you need one more () to run it. Both preset arguments; only thunkify delays the call.
R.thunkify(fn) waits for fn's arguments. Once they are all supplied, you get back a zero-argument function (the thunk). The final () calls that thunk, which applies the saved arguments to fn.
No. Every thunk() call runs fn again with the same saved arguments. To run the work only once, wrap the thunk with R.once: R.once(R.thunkify(fn)(a)).
No. JavaScript evaluates arguments when you pass them, so R.thunkify(f)(heavy()) runs heavy() immediately. Only the call to f is delayed. Put the expensive work inside f if it must wait.
No. R.thunkify has been a stable Ramda function helper since v0.26.0 — not deprecated, experimental, or non-standard.

Next: R.tryCatch()

Learn how to run a fallback function when another one throws.

R.tryCatch() 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.

6 people found this page helpful