Lodash _.delay() method
What you’ll learn
- How to run callbacks after explicit wait times with
_.delay. - How argument forwarding works.
- How delay differs from defer and plain timers.
- How to inspect return timer ids.
Prerequisites
Optional read: _.defer() and the Function hub.
- Timers: know the basics of milliseconds and async callbacks.
- Callback args: passing values into delayed functions.
Overview
_.delay(func, wait, ...args) schedules func after a chosen wait period. It forwards args and returns a timer id, giving a small wrapper around setTimeout.
Syntax
javascript
_.delay(func, wait, ...args)- func: function to run later.
- wait: milliseconds before invocation.
- args: optional values forwarded to
func. - returns: timer id number.
1
Basic delayed call
Runs callback after a known wait.
javascript
import delay from "lodash/delay";
delay(() => console.log("later"), 400);
// logs after ~400ms2
Pass arguments
Arguments after wait are provided to callback on execution.
javascript
import delay from "lodash/delay";
delay(function (text) {
console.log(text);
}, 300, "later");
// "later"3
Inspect timer id
Delay returns timer id values like setTimeout.
javascript
import delay from "lodash/delay";
const id = delay(() => {}, 100);
console.log(typeof id);
// "number"📋 _.delay vs _.defer
| Helper | Timing | Use case |
|---|---|---|
_.delay | Custom wait in ms | Scheduled callbacks with explicit delay |
_.defer | Next tick style | Run after current synchronous stack |
Pitfalls to avoid
Units
Milliseconds confusion
Wait values are ms, not seconds.
Lifecycle
Forgotten cleanup
Pending delayed work can fire after component teardown.
Coercion
Invalid waits
Non-numeric waits coerce to 0 and still defer asynchronously.
❓ FAQ
It invokes func after the specified wait milliseconds and forwards any additional arguments to func.
It returns a timer id number, similar to setTimeout.
delay accepts a custom wait duration; defer always schedules after the current stack with a tiny fixed delay.
Yes. A 0 wait still defers execution asynchronously to the timer queue.
Use delay for consistency when a codebase already uses Lodash wrappers and you want variadic arg forwarding in the same API style.
Summary
- Purpose:
_.delayschedules a function after an explicit wait. - Extras: forwards args and returns timer id.
- Next: Lodash _.flip(), revisit Lodash _.defer(), or read official _.delay docs.
Did you know?
Lodash implements _.delay with baseDelay(func, toNumber(wait) || 0, args), so non-numeric waits coerce to 0 and still schedule asynchronously with passed arguments.
6 people found this page helpful
