Underscore.js _.defer() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

Defers invoking the function until the current call stack has cleared (official Underscore docs). Useful for expensive work without blocking the UI, or scheduling after DOM updates. Extra arguments are forwarded. Stable Function helper — not deprecated.

Pass the required arguments shown below. Underscore function helpers usually return a new function — check timing and call-count behavior carefully.

Syntax

Call _.defer. Start with this line.

Underscore
_.defer(function, *arguments)
CallWhat it does
_.defer(function, *arguments)Primary signature.
_.defer(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.defer(function, *arguments)

Parameters

ParameterDescription
functionRequired. Function to run after the stack clears
*argumentsOptional. Forwarded to the function when it runs

Return Value

Return ValueDescription
*Timer id (cancel with clearTimeout)

Underscore.js Examples

Example 1 — Call Order (Official Idea)

Sync code finishes; then the deferred function runs.

Underscore
var log = [];

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

log.push("first");
// right after sync: ["first"]
// after stack clears: ["first","second"]
Try It Yourself

How It Works

  1. _.defer schedules the push of "second".
  2. Sync pushes "first" immediately.
  3. Stack clears → deferred callback runs.
  4. Same idea as the official alert example.

Example 2 — Forward Arguments

Extra args after the function are passed when it runs.

Underscore
var log = [];

_.defer(function (a, b) {
  log.push(a + " " + b);
}, "hello", "moe");

// after stack clears: log → ["hello moe"]
Try It Yourself

How It Works

  1. First arg is the function to defer.
  2. "hello", "moe" are stored for later.
  3. When it runs, they become a and b.
  4. Same forwarding pattern as _.delay.

Example 3 — Cancel with clearTimeout

Save the timer id and cancel before the deferred call fires.

Underscore
var log = [];
var id = _.defer(function () {
  log.push("ran");
});

clearTimeout(id);
// log stays [] — deferred call never ran
Try It Yourself

How It Works

  1. _.defer returns a timer id.
  2. clearTimeout(id) removes the pending call.
  3. fn never runs — log stays empty.
  4. Useful if the view/page is torn down first.

Example 4 — Defer vs Delay

Yield ASAP vs wait a chosen number of milliseconds.

Underscore
// After DOM update / yield to paint:
_.defer(measureLayout);

// Wait a specific time:
_.delay(showToast, 2000);

// Rule of thumb:
// defer → "as soon as the stack is free"
// delay → "after N milliseconds"
Try It Yourself

How It Works

  1. Defer ≈ _.delay(fn, 1) — minimal wait.
  2. Delay needs an explicit wait.
  3. UI yield / post-DOM → _.defer.
  4. Timed UX → _.delay.

Browser & Runtime Support

_.defer ships with Underscore.js (1.x). It uses timers (setTimeout / clearTimeout) and runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.defer()

Available in [email protected] (UMD / ESM). Equivalent to a near-zero delay. Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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 Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.defer(function, *arguments) waits until the current call stack has cleared, then runs the function once — like setTimeout(fn, 0). Extra arguments are forwarded when it runs.
_.defer(fn) is essentially _.delay(fn, 1) — it yields ASAP. Use _.defer to let the browser breathe; use _.delay when you need a specific wait in milliseconds.
Same idea under the hood. _.defer is clearer for 'run after current work' and forwards args: _.defer(fn, a, b) runs fn(a, b).
So the browser can finish painting or handling input first. Handy after DOM updates or for splitting heavy work without freezing the UI.
Yes — a timer id (like setTimeout). Cancel with clearTimeout(id) before it fires.
No. Like setTimeout, this is not preserved. Bind first with _.bind or pass an already-bound function.

Next: _.throttle()

Create a throttled version of a function that runs at most once per wait.

_.throttle() 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.

10 people found this page helpful