Ramda.js R.andThen() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Applies a function to the value inside a successfully resolved Promise — curried p.then(onSuccess) for R.pipe. Part of Ramda Function since v0.27.1 (stable — not deprecated).

Note: Renamed from R.then so a Ramda helper would never look like a Promise thenable. Prefer R.andThen in all new code.

Syntax

Call R.andThen. Start with this line.

Ramda
R.andThen(onSuccess, p) or R.andThen(onSuccess)(p)
CallWhat it does
R.andThen(onSuccess, p) or R.andThen(onSuccess)(p)Primary signature.
R.andThen(...)Call the function helper.

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

Library declarations
Ramda
R.andThen(onSuccess, p) or R.andThen(onSuccess)(p)

Parameters

ParameterDescription
onSuccessRequired. Function applied to the resolved value (may return a value or a Promise)
pRequired. A Promise / thenable

Return Value

Return ValueDescription
*A Promise: p.then(onSuccess)

Ramda Examples

Example 1 — Official getMemberName

Query → fetch → pick name fields after the Promise resolves.

Ramda
const makeQuery = email => ({ query: { email } });
const fetchMember = () =>
  Promise.resolve({ firstName: "Bob", lastName: "Loblaw", id: 42 });
const pickName = R.pick(["firstName", "lastName"]);

const getMemberName = R.pipe(
  makeQuery,
  fetchMember,
  R.andThen(pickName)
);

getMemberName("[email protected]");
// Promise → { firstName: "Bob", lastName: "Loblaw" }
Try It Yourself

How It Works

  1. Build the query. makeQuery is sync and returns a plain object.
  2. Fetch. fetchMember returns a Promise of the member (stubbed here).
  3. Wait, then pick. R.andThen(pickName) runs only after resolve.
  4. Result. You get a Promise of { firstName, lastName } — the official sample outcome.

Example 2 — Increment a Promise

Smallest useful call: unwrap a Promise, then run a Ramda helper.

Ramda
R.andThen(R.inc)(Promise.resolve(41));
// Promise → 42

R.andThen(R.toUpper)(Promise.resolve("hi"));
// Promise → "HI"
Try It Yourself

How It Works

  1. Start resolved. Promise.resolve(41) holds the number 41.
  2. Curry the helper. R.andThen(R.inc) waits for that Promise.
  3. Apply. On resolve, R.inc(41) → 42.
  4. Same idea for strings. R.toUpper runs on "hi" after unwrap.

Example 3 — Why Pipes Need andThen

R.pick on a Promise object does not wait — you get {}, not Bob’s name.

Ramda
const member = { firstName: "Bob", lastName: "Loblaw", id: 42 };
const pickName = R.pick(["firstName", "lastName"]);

pickName(Promise.resolve(member));
// → {}   (ran on the Promise object)

R.andThen(pickName)(Promise.resolve(member));
// → Promise of { firstName: "Bob", lastName: "Loblaw" }
Try It Yourself

How It Works

  1. A Promise is an object. It has no firstName key.
  2. Bare pick fails. R.pick runs immediately → empty object.
  3. andThen delays. It waits for resolve, then calls pickName.
  4. Rule. After any Promise step in a pipe, wrap the next function in R.andThen.

Example 4 — Chain Two andThen Steps

After the first Promise, wrap each later transform. Here: plus one, then times two.

Ramda
const run = R.pipe(
  R.always(Promise.resolve(10)),
  R.andThen(R.inc),
  R.andThen(R.multiply(2))
);

run();
// Promise → 22   (10 + 1, then × 2)
Try It Yourself

How It Works

  1. Seed a Promise. R.always(Promise.resolve(10)) starts the pipe.
  2. First then. R.andThen(R.inc) → Promise of 11.
  3. Second then. R.andThen(R.multiply(2)) → Promise of 22.
  4. Bulk option. Docs also show R.pipeWith(R.andThen, [f, g, h]) to insert andThen between every function.

Example 5 — Flatten a Nested Promise

If onSuccess returns another Promise, the result flattens like native .then.

Ramda
const fetchId = () => Promise.resolve(42);
const fetchUser = id => Promise.resolve({ id, name: "Bob" });

R.andThen(fetchUser)(fetchId());
// Promise of { id: 42, name: "Bob" }
// not Promise of Promise
Try It Yourself

How It Works

  1. First Promise. fetchId() resolves to 42.
  2. Second Promise. fetchUser(42) returns another Promise.
  3. Flatten. R.andThen waits for both, then yields the user object.
  4. Same as native. Behavior matches Promise.prototype.then.

Browser & Runtime Support

R.andThen is a Ramda Function helper since v0.27.1. It delegates to Promise.prototype.then, so it needs Ramda 0.27.1+ and a Promise implementation (native in modern browsers and Node).

✓ Stable · Ramda 0.27.1+

R.andThen()

Works wherever Ramda 0.27.1+ and Promises are available — evergreen browsers (CDN or bundle), Node.js, Deno, and Bun. Internet Explorer needs a Promise polyfill.

100% With Ramda 0.27.1+ & Promises
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.andThen Ramda 0.27.1+

Bottom line: Safe for production once Ramda 0.27.1+ and Promises are present. Load via CDN (ramda.min.js) or npm. Pair with R.otherwise for failures. Official reference: ramdajs.com/docs/#andThen.

Frequently Asked Questions

R.andThen(onSuccess, p) returns p.then(onSuccess). It applies onSuccess to the value inside a successfully resolved Promise. It is curried so it fits inside R.pipe.
getMemberName = R.pipe(makeQuery, fetchMember, R.andThen(pickName)). Calling getMemberName('[email protected]') resolves to { firstName: 'Bob', lastName: 'Loblaw' }. Added in v0.27.1. See also R.otherwise and R.pipeWith.
R.pipe passes each return value immediately. fetchMember returns a Promise object, not the member. R.pick on that Promise misses firstName/lastName and often yields {}. R.andThen waits, then runs pick.
Yes. Like Promise.prototype.then, if onSuccess returns a Promise, R.andThen flattens it to one Promise of the inner value.
R.andThen only runs on success. Pair it with R.otherwise (curried .catch) for the failure path.
No. It is a stable Ramda Function helper from v0.27.1. It replaced the older name R.then so it would not clash with Promise thenables. It is not deprecated, experimental, or non-standard.

Next: R.ap()

Apply a list of functions to a list of values — Ramda’s applicative apply.

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

11 people found this page helpful