Lodash _.defer() method

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

What you’ll learn

  • How _.defer schedules work after current synchronous code completes.
  • How to pass arguments into deferred callbacks.
  • When to use _.defer vs _.delay or direct timers.
  • How to validate deferred ordering in simple examples.

Prerequisites

Helpful context: _.debounce() for scheduling concepts and the Function hub.

  • Event loop basics: timers run later than sync statements.
  • Function arguments: forwarding values to callbacks.

Overview

_.defer(func, ...args) defers execution until after the current call stack clears. It behaves like a tiny timeout helper and returns the timer id.

Syntax

javascript
_.defer(func, ...args)
  • func: function to execute later.
  • args: optional arguments passed to func.
  • returns: timer id number.
1

Deferred ordering

Synchronous logs run first; deferred callback logs after stack clears.

javascript
import defer from "lodash/defer";

console.log("start");
defer(() => console.log("deferred"));
console.log("end");
// start
// end
// deferred
2

Passing arguments

Additional values after func are forwarded into the deferred callback.

javascript
import defer from "lodash/defer";

defer(function (text) {
  console.log(text);
}, "deferred");
// logs "deferred"
3

Timer id return

_.defer returns a timer id, useful when coordinating with timer APIs.

javascript
import defer from "lodash/defer";

const id = defer(() => console.log("later"));
console.log(typeof id);
// "number"

📋 _.defer vs _.delay

HelperWait controlUse case
_.deferFixed next tick style delayRun after synchronous setup
_.delayCustom millisecondsRun after explicit time

Pitfalls to avoid

Expectations

Not immediate

defer never runs inline; do not rely on return values from deferred callbacks.

Microtasks

Different queue

Promise callbacks run in microtask timing, which may execute before timer-based defer callbacks.

Cleanup

Abandoned timers

Track ids if lifecycle teardown requires clearing pending work.

❓ FAQ

It schedules func to run after the current call stack clears, passing any extra arguments to func.
It returns a timer id (number), similar to setTimeout.
defer always waits for the next tick with a tiny delay, while delay lets you provide a custom wait in milliseconds.
Use it when synchronous setup must finish before running follow-up code, for example after state mutations or DOM preparation.
No. defer uses timer-based scheduling (macrotask timing), not microtask queues.

Summary

Did you know?

In Lodash 4.x, _.defer is implemented as baseDelay(func, 1, args) via baseRest, so execution is always deferred until after the current synchronous stack completes.

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