Lodash _.after() method
What you’ll learn
- How
_.after(n, func)builds a wrapper that ignoresfuncuntil enough calls arrive. - Why pairing
nwithsaves.length(or task counts) models “all async jobs finished.” - How early calls differ from the first real invocation (return values and
thisforwarding). - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Optional read Lodash Function methods hub; you should be comfortable passing functions as arguments.
- Closures: the wrapper remembers how many times it was invoked.
Function.prototype.apply: Lodash forwardsthisandargumentsonce the guard opens.
Overview
_.after(n, func) returns a new function. Each time that wrapper runs, Lodash decrements an internal counter derived from n; only when the counter falls below one does it call func with the current this and arguments. Until then the wrapper effectively no-ops (returns undefined).
Barrier pattern
Hand the same wrapper to every parallel completion path; the final caller triggers func once everyone checked in.
Opposite of before
_.before stops invoking after a limit; after withholds invocation until a threshold is crossed.
Tree-shakeable
Import lodash/after alone when this guard is all you need.
Syntax
_.after(n, func) - n: numeric threshold coerced with
toInteger; controls how many wrapper calls occur beforefuncruns. - func: must be a function or Lodash throws
TypeError: Expected a function. - Returns: a new wrapper function forwarding
thisand arguments once the guard passes; earlier calls returnundefined.
Parallel “done” callback
Pass one shared wrapper into every completion slot—mirrors the official Lodash docs pattern where n matches how many branches must finish.
import after from "lodash/after";
const saves = ["profile", "settings"];
const done = after(saves.length, () => {
console.log("done saving!");
});
saves.forEach(() => {
done(); // synchronous stand-in for asyncSave(..., complete: done)
}); Third call runs
With after(3, fn), the first two invocations decrement the guard only; the third finally executes fn and returns its result.
import after from "lodash/after";
const logThird = after(3, () => "third!");
logThird();
// undefined
logThird();
// undefined
logThird();
// "third!" after(1, fn) on first call
When n is 1, the guard opens immediately on the first invocation—useful when you want identical ergonomics as other barriers but only one signal.
import after from "lodash/after";
const run = after(1, (msg) => msg.toUpperCase());
run("hello");
// "HELLO" 📋 _.after vs _.before
| Helper | When func runs | Typical use |
|---|---|---|
_.after(n, func) | Starting at the nth wrapper call | Barrier after N completions (often n = tasks.length) |
_.before(n, func) | Only while count < n; then freezes | Cap clicks or retries (first n - 1 calls invoke func) |
Need exactly-once semantics forever? Combine patterns with _.once; after alone keeps calling func on every invocation after the gate opens.
Pitfalls to avoid
Wrong n
If completions fire fewer than n times, func never runs; too many duplicate signals fire early.
n = 0Immediate passthrough
Coerced zero removes the guard immediately—double-check dynamic lengths.
Extra invocations
After the barrier opens, every further call still hits func; gate again manually if you need exactly once.
❓ FAQ
Summary
- Purpose:
_.after(n, func)returns a wrapper that callsfunconly after at leastninvocations. - Contrast:
_.beforecaps how many timesfuncmay run;afterdelays the first run. - Next: Lodash _.ary(), Function methods hub, or official Lodash docs for _.after.
Lodash positions _.after as the counterpart to _.before: after waits until the wrapper has been invoked at least n times, while before only lets func run during the first n - 1 invocations.
6 people found this page helpful
