Lodash _.defer() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Defers invoking func until the current call stack has cleared. Extra arguments are forwarded to func. Returns a timer id. Part of Lodash 4.x Function (since 0.1.0).

Pass the required arguments shown below. Most helpers return a new function you call later.

Syntax

Call _.defer. Start with this line.

Lodash
_.defer(func, [args])
CallWhat it does
_.defer(func, [args])Primary signature.
_.defer(func)Wrap or transform a function.

Prefer import defer from "lodash/defer" when you want a smaller bundle.

Library declarations
Lodash
_.defer(func, [args])

Parameters

ParameterDescription
funcRequired. Function to run after the stack
argsOptional. Arguments forwarded to func

Return Value

Return ValueDescription
*Timer id (number)

Lodash Examples

Example 1 — Official: Defer a Log

Schedule a callback with one argument.

Lodash
var message = "";

_.defer(function (text) {
  message = text;
}, "deferred");

// after stack clears → message === "deferred"
Try It Yourself

How It Works

  1. Schedule. Lodash queues the function with "deferred".
  2. Stack. Current sync code finishes first.
  3. Run. Callback sets message to "deferred".
  4. Docs. Same idea as logging after ~1 ms.

Example 2 — Sync Code Runs First

Prove deferred work happens after the current stack.

Lodash
var steps = [];

_.defer(function () {
  steps.push("deferred");
});

steps.push("sync");

// immediately: steps → ["sync"]
// after defer:  steps → ["sync", "deferred"]
Try It Yourself

How It Works

  1. Defer. Callback is scheduled, not run yet.
  2. Sync. "sync" is pushed immediately.
  3. Later. Deferred callback pushes "deferred".
  4. Order. Always sync first, then deferred.

Example 3 — Forward Multiple Arguments

Every value after func is passed through.

Lodash
var result = null;

_.defer(function (a, b) {
  result = a + " " + b;
}, "hello", "world");

// after defer → result === "hello world"
Try It Yourself

How It Works

  1. Args. "hello" and "world" are stored with the timer.
  2. Invoke. Callback receives them as a and b.
  3. Join. Result becomes "hello world".
  4. Same as. Official single-arg pattern, just more values.

Example 4 — Timer Id and Cancel

_.defer returns a timer id you can clear.

Lodash
var ran = false;

var id = _.defer(function () {
  ran = true;
});

clearTimeout(id);

// after wait → ran still false
Try It Yourself

How It Works

  1. Return. id is a timer handle (number).
  2. Cancel. clearTimeout(id) drops the scheduled call.
  3. Result. ran stays false.
  4. Tip. Cancel on teardown if the page may leave early.

Example 5 — defer vs delay

Choose next-tick deferral or an explicit wait.

Lodash
var log = [];

_.defer(function () {
  log.push("defer");
});

_.delay(function () {
  log.push("delay");
}, 50);

// soon: ["defer"] then later ["defer","delay"]
Try It Yourself

How It Works

  1. defer. Runs as soon as the stack is clear (~1 ms).
  2. delay. Waits the given 50 ms.
  3. Order. "defer" appears first, then "delay".
  4. Pick. Use delay when timing matters to the user.

Browser & Runtime Support

_.defer is a Lodash 4.x Function helper (since 0.1.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Built on timer APIs similar to setTimeout.

✓ Stable · Lodash 0.1+

_.defer()

Available in [email protected] full builds and the lodash/defer modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.defer Lodash 0.1+

Bottom line: Safe for production once Lodash is loaded. Prefer import defer from "lodash/defer" for smaller builds. Use when you need work after the current stack, not a user-facing delay.

Frequently Asked Questions

_.defer(func, ...args) schedules func to run after the current call stack has cleared. Extra args are passed to func.
A timer id (number), like setTimeout — you can clearTimeout it if needed.
defer always waits until the stack clears (tiny timer). delay lets you choose any wait in milliseconds.
No. It uses timer scheduling (macrotask), not Promise.then / queueMicrotask.
When synchronous setup must finish first — then run a follow-up without blocking the current stack.
Officially about one millisecond after the stack clears — not a user-chosen delay.

Next: _.delay()

Invoke a function after a chosen number of milliseconds.

_.delay() tutorial →

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.

9 people found this page helpful