Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.
try...catch
block to return either the result of the function or an appropriate error.Error
, use it to create a new Error
.const attempt = (fn, ...args) => {
try {
return fn(...args);
} catch (e) {
return e instanceof Error ? e : new Error(e);
}
};
var elements = attempt(function(selector) {
return document.querySelectorAll(selector);
}, '>_>');
if (elements instanceof Error) elements = []; // elements = []
JavaScript, Function
Creates a function that invokes each provided function with the arguments it receives and returns the results.
JavaScript, Function
Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.
JavaScript, Function
Creates a function that invokes the provided function with its arguments transformed.