How to use the attempt function from lodash

Find comprehensive JavaScript lodash.attempt code examples handpicked from public code repositorys.

lodash.attempt attempts to invoke a function and returns either the result of the function or the error that was thrown.

25
26
27
28
29
30
31
32
33
34
module.exports.assign              = _.assign;
module.exports.assignIn            = _.assignIn;
module.exports.assignInWith        = _.assignInWith;
module.exports.assignWith          = _.assignWith;
module.exports.at                  = _.at;
module.exports.attempt             = _.attempt;
module.exports.before              = _.before;
module.exports.best                = _.best;
module.exports.binPick             = _.binPick;
module.exports.binary              = _.binary;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

877
878
879
880
881
882
883
884
885
886
887
888
889
890
console.log(words); // => ['fred', 'barney', 'pebbles']




// Util


const attempt = _.attempt(function() { return _.divide(1, 0); });
console.log(attempt); // => [Error: Divide by zero.]


const bindAll = _.bindAll(object, ['increment', 'square']);
console.log(bindAll); // => { 'increment': [Function: increment], 'square': [Function: square] }
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.attempt work?

lodash.attempt is a method that invokes a given function and returns either the result of the function or the error object if it fails, without throwing the error. It is useful when you need to handle errors gracefully and avoid stopping the execution of your program. Internally, lodash.attempt uses a try-catch block to execute the provided function, catching any error that might be thrown. If the function executes successfully, the result is returned, otherwise the error object is returned.

90
91
92
93
94
95
96
97
98
99
  it('should create a generic error, if I/O error is unknown', () => {
    expect(builder.failedToReadConfiguration()).toMatchSnapshot();
  });

  it('should create a simple error, but with the original intercepted IO error', () => {
    const ioError = _.attempt(() => fs.readFileSync(os.homedir()));
    delete ioError.stack;
    expect(builder.failedToReadConfiguration(ioError)).toMatchSnapshot();
  });
});
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const _ = require("lodash");

function divide(a, b) {
  return a / b;
}

const result = _.attempt(divide, 10, 0);

if (_.isError(result)) {
  console.error(result);
} else {
  console.log(result);
}

In this example, lodash.attempt is used to call the divide function, which attempts to divide the first argument by the second argument. Because the second argument is zero, this would normally throw a TypeError in JavaScript. However, because we're using lodash.attempt, any errors thrown by the function are caught and returned as an error object. In this case, the result variable will be an error object, because the divide function throws an error. We can use _.isError to check whether result is an error object, and then handle it appropriately.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)