Attempts to invoke func, returning either the result or the caught error object. Extra arguments are passed to func. Part of Lodash Util since 3.0.0 (stable — not deprecated).
Note: Synchronous only — it does not await Promises. Pair the return value with _.isError().
Syntax
Call _.attempt. Start with this line.
Lodash
_.attempt(func, [args])
Call
What it does
_.attempt(func, [args])
Primary signature.
_.attempt(...)
Call the util helper.
Prefer import attempt from "lodash/attempt" when you want a smaller bundle.
Library declarations
Lodash
_.attempt(func, [args])
Parameters
Parameter
Description
func
Required. Function to invoke
args
Optional. Arguments forwarded to func
Return Value
Return Value
Description
*
func result, or the caught error object
Lodash Examples
Example 1 — Official: Invalid Selector
Catch a DOM exception and fall back to an empty list.
Lodash
var elements = _.attempt(function (selector) {
return document.querySelectorAll(selector);
}, '>_>');
if (_.isError(elements)) {
elements = [];
}
console.log(elements);
// → []
Parse._.attempt(JSON.parse, raw) forwards the string.
Valid. Good JSON returns the object.
Invalid. Bad JSON throws → attempt returns the Error.
Fallback._.isError picks a default config.
Example 4 — Thrown Error as a Value
Any thrown Error becomes the return value — your script keeps running.
Lodash
function risky() {
throw new Error('Something went wrong');
}
var result = _.attempt(risky);
if (_.isError(result)) {
console.log(result.message);
}
// → "Something went wrong"
Row 2. Missing payload throws → Error in the array.
Continue. The map finishes; partition successes later.
Compatibility
Browser & Runtime Support
_.attempt is a Lodash Util helper since 3.0.0. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.
✓ Stable · Lodash 3.0.0+
_.attempt()
Available in [email protected] full builds and the lodash/attempt modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.
100%With Lodash 3.0+
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
_.attemptLodash 3.0.0+
Bottom line: Safe for production once Lodash is loaded. Prefer import attempt from "lodash/attempt" for smaller bundles. Use it for small synchronous risky calls; use native async/await for Promises.
Frequently Asked Questions
_.attempt(func, …args) invokes func and returns its result, or the caught error object if it throws. Official use: wrap risky calls like document.querySelectorAll with an invalid selector.
Use _.isError(result). When true, treat result as the caught Error (read result.message). When false, result is the normal return value.
Yes. Extra arguments after func are forwarded: _.attempt(JSON.parse, text) calls JSON.parse(text).
try/catch is a statement block. _.attempt returns value-or-error as one expression, which is handy inside _.map() or other callbacks.
No. It only catches synchronous throws. Promise rejections need try/catch with await, or .catch().
Use import attempt from "lodash/attempt" or require("lodash/attempt"). Full build: import _ from "lodash" then _.attempt.