Creates a function that invokes func while it is called less than n times. Later calls return the last func result. Part of Lodash 4.x Function (since 3.0.0).
Pass the required arguments shown below. Most helpers return a new function you call later.
Syntax
Call _.before. Start with this line.
Lodash
_.before(n, func)
Call
What it does
_.before(n, func)
Primary signature.
_.before(func)
Wrap or transform a function.
Prefer import before from "lodash/before" when you want a smaller bundle.
Library declarations
Lodash
_.before(n, func)
Parameters
Parameter
Description
n
Required. Call count where func stops (coerced with toInteger)
func
Required. Function to restrict; throws if not a function
Return Value
Return Value
Description
*
New restricted function
Lodash Examples
Example 1 — Basic before(3)
Func runs on the first two calls only.
Lodash
var count = 0;
var limited = _.before(3, function () {
count += 1;
return count;
});
[limited(), limited(), limited(), limited()];
// → [1, 2, 2, 2]
var early = [];
var late = [];
var b = _.before(3, function (x) { early.push(x); });
var a = _.after(3, function (x) { late.push(x); });
[1, 2, 3, 4].forEach(function (x) {
b(x);
a(x);
});
early; // → [1, 2]
late; // → [3, 4]
_.before is a Lodash 4.x Function helper (since 3.0.0). It needs no special browser APIs beyond loading Lodash.
✓ Stable · Lodash 3.0.0+
_.before()
Available in [email protected] full builds and the lodash/before modular import. Works in evergreen browsers and Node / Bun / Deno when Lodash is installed.
100%With Lodash 4.x
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.beforeLodash 4.x+
Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import before from "lodash/before" for smaller builds.
Frequently Asked Questions
_.before(n, func) returns a function that invokes func while the call count is less than n (at most n−1 times). Later calls return the last result without calling func again.
before runs early (first n−1 calls) then freezes. after waits until call n, then runs func on that call and every later call.
At most n − 1 times. _.before(5, fn) runs fn up to 4 times — matching the official click-cap example.
func never runs. On the first call the counter drops to 0 before the --n > 0 guard can pass, so the result stays undefined.
_.once(func) is like running at most once. _.before(2, func) also invokes func on the first call and freezes afterward.
Use import before from "lodash/before" or require("lodash/before") for tree-shaking. Full build: import _ from "lodash" then _.before.