Lodash _.before() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.before(n, func) counts wrapper invocations and stops calling func after the limit.
  • Why the documented before(5, ...) pattern caps real work at four executions.
  • What callers observe once the wrapper freezes (cached return value).
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Optional reads Function hub, _.after(), and _.ary(); helpful to contrast guards that delay versus cap execution.

  • Higher-order functions: storing mutable state in closures between calls.
  • Return values: understanding that skipped invocations still return the last memoized result.

Overview

_.before(n, func) wraps func so it executes only while the wrapper’s internal counter (derived from n) stays positive after each decrement. Crossing the threshold freezes the last computed result and prevents further executions—ideal for UI caps such as “only four saves allowed.”

Hard stop

After enough clicks or callbacks, downstream logic cannot trigger func again through the same wrapper.

Memoized exit

Late callers still receive the final successful return without recomputation.

Tree-shakeable

Import lodash/before when you only need this guard.

Syntax

javascript
_.before(n, func)
  • n: threshold coerced with toInteger; determines when func stops running (after n - 1 successful calls).
  • func: must be a function or Lodash throws TypeError: Expected a function.
  • Returns: a wrapper that forwards this and arguments while calls remain allowed; afterward returns the frozen result.
1

Click-style cap (Lodash doc pattern)

before(5, fn) mirrors the official docs: the underlying work executes at most four times—the fifth press replays the fourth outcome without calling fn again.

javascript
import before from "lodash/before";

let count = 0;
const addContact = before(5, () => {
  count += 1;
  return count;
});

addContact(); // 1
addContact(); // 2
addContact(); // 3
addContact(); // 4
addContact(); // still 4 — frozen
Try it Yourself
2

Side effects halt

Anything inside func—logging, DOM writes, network posts—runs only until the guard closes; extra wrapper calls short-circuit.

javascript
import before from "lodash/before";

const logTwice = before(3, (msg) => {
  console.log(msg);
  return msg.toUpperCase();
});

logTwice("a"); // prints, returns "A"
logTwice("b"); // prints, returns "B"
logTwice("c"); // silent — returns "B"
Try it Yourself
3

before(2, fn) runs once

Smallest useful threshold: first invocation executes func; every later call replays that singleton outcome.

javascript
import before from "lodash/before";

const stamp = before(2, () => ({ id: Math.random() }));

const first = stamp();
const second = stamp();

first === second;
// true — same object reference returned
Try it Yourself

📋 _.before vs _.after

HelperExecution windowMental model
_.before(n, func)First n - 1 wrapper calls“Stop inviting work after enough tries.”
_.after(n, func)Starting at call n onward“Wait for everyone to finish before continuing.”

Need forever-once semantics without caring about counts? Prefer _.once.

Pitfalls to avoid

n = 1

Nothing runs

The guard immediately blocks func; bump n or switch helpers if you expected one execution.

Off-by-one

Miscounting allowances

Remember allowable executions are n - 1, not n, matching the Lodash documentation wording.

Stale result

Ignoring new inputs

After freezing, fresh arguments are discarded—design APIs accordingly.

❓ FAQ

At most n - 1 times. The nth wrapper call skips func and returns the last stored result; afterward func is cleared internally.
The inner function never runs: on the first invocation n becomes 0 before the guard --n > 0 can pass, so result stays undefined.
after waits until the wrapper has been hit n times before delegating to func on subsequent calls. before lets func run early but stops calling it once the limit is reached while replaying the final return value.
No external mutation—the wrapper closes over a slot Lodash sets to undefined after freezing—but callers must keep using the wrapped function to get the guarded behavior.
Use import before from "lodash/before" for tree-shaking friendly bundles.

Summary

Did you know?

Once the internal counter reaches Lodash’s cutoff, before assigns func to undefined inside the closure—dropping the reference helps garbage-collection while later calls still return the memoized result.

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