Lodash flowRight Argument Handling Explained

The _.flowRight method in Lodash allows developers to compose functions in a right-to-left execution order, where the return value of each function is passed as the argument to the next. When invoking the composed function, the initial function in the sequence—the rightmost function—has unique behavior: it can accept multiple arguments or an array of arguments directly, while all subsequent upstream functions only receive a single return value. This article explains how _.flowRight processes an initial array of arguments and delivers them to the first evaluated function.

Right-to-Left Execution Flow

_.flowRight is Lodash's implementation of the traditional mathematical compose utility. When you pass a list of functions _.flowRight([f, g, h]), the execution order is h first, followed by g, and finally f.

Because h is the entry point of the pipeline, it directly receives whatever arguments are supplied when the returned composite function is called.

How the First Evaluated Function Receives Arguments

When you call a composed function created by _.flowRight, Lodash delegates the arguments using internal invocation methods equivalent to Function.prototype.apply. Consequently, the first evaluated function receives the exact argument list provided at call time.

How an array is processed depends entirely on how it is passed to the composite function:

1. Passing an Array as a Single Argument

If an array is passed as an individual argument, the first function receives that array as its first parameter:

const _ = require('lodash');

const sumArray = (arr) => arr.reduce((acc, val) => acc + val, 0);
const double = (n) => n * 2;

// sumArray is the rightmost, so it executes first
const processNumbers = _.flowRight([double, sumArray]);

const result = processNumbers([1, 2, 3, 4]);
// sumArray receives [1, 2, 3, 4]
// Returns 10 -> double(10) -> 20
console.log(result); // Output: 20

In this scenario, Lodash does not automatically flatten, destructure, or spread the array. The array remains intact as a single argument.

2. Passing an Array via Spread or apply

If an array contains multiple arguments that you want to map to individual parameters of the first function, you must spread the array when invoking the composite function or use .apply():

const _ = require('lodash');

const multiplyThree = (a, b, c) => a * b * c;
const formatResult = (val) => `Total: ${val}`;

const compute = _.flowRight([formatResult, multiplyThree]);

const args = [2, 3, 4];

// Spreading the array passes elements as distinct arguments
const result = compute(...args);
// multiplyThree receives (2, 3, 4)
// Returns 24 -> formatResult(24) -> "Total: 24"
console.log(result); // Output: Total: 24

Lodash captures all forwarded parameters (arguments) using apply(this, args) under the hood. As a result, multiplyThree receives each item in the array as a separate argument according to its index.

Behavior of Subsequent Functions

While the initial (rightmost) function can receive an arbitrary number of arguments or an array of arguments, subsequent functions in the chain cannot. In standard JavaScript function composition, functions can only return a single value. Therefore:

  1. The first function processes the initial arguments (array or otherwise).
  2. The first function returns a single value.
  3. Every subsequent function to the left receives only that single return value as its input.

If subsequent functions must process multiple values, the preceding functions must explicitly return those values inside a single structure, such as an array or an object.