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)
Call
What 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
Parameter
Description
function
Required. Function to run after the stack clears
*arguments
Optional. Forwarded to the function when it runs
Return Value
Return Value
Description
*
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"]
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"
_.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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.deferUnderscore 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.