A function that tries to execute a function and discards any error that occurs
npx @tessl/cli install tessl/npm-nice-try@4.0.0A utility function that tries to execute a function and discards any error that occurs, returning undefined instead of throwing. The package provides both synchronous and asynchronous variants for safe error suppression in JavaScript applications.
npm install nice-tryimport niceTry from 'nice-try';For the promises variant:
import niceTry from 'nice-try/promises';import niceTry from 'nice-try';
// Safe JSON parsing - returns parsed value or undefined
const result = niceTry(() => JSON.parse('{"valid": true}'));
console.log(result); // { valid: true }
// Invalid JSON - returns undefined instead of throwing
const invalid = niceTry(() => JSON.parse('invalid json'));
console.log(invalid); // undefined
// Works with any function that might throw
const safeOperation = niceTry(() => {
// Some operation that might fail
return someRiskyFunction();
});For asynchronous operations:
import niceTry from 'nice-try/promises';
// Safe async operations
const asyncResult = await niceTry(async () => {
const response = await fetch('/api/data');
return response.json();
});
// Returns undefined if the fetch or parsing fails
console.log(asyncResult); // parsed data or undefinednice-try is built around a dual-entry pattern providing both synchronous and asynchronous error suppression:
nice-try): Synchronous function that catches errors in regular functionsnice-try/promises): Asynchronous function that catches errors in async functions and promise rejectionsundefined returnsundefinedThis design allows developers to choose the appropriate variant based on whether they're working with synchronous or asynchronous operations while maintaining a consistent API experience.
Executes a synchronous function and silently catches any errors that occur.
/**
* Executes the provided function and returns its result.
* If an error is thrown during execution, the error is silently ignored and undefined is returned.
*
* @param {Function} fn - The function to execute.
* @returns {*} The return value of the function, or undefined if an error occurred.
*/
export default function niceTry(fn);Usage Examples:
import niceTry from 'nice-try';
// JSON parsing
const data = niceTry(() => JSON.parse(jsonString));
// Property access that might fail
const value = niceTry(() => obj.deeply.nested.property);
// Function call that might throw
const result = niceTry(() => riskyFunction());
// Invalid input handling
const invalid = niceTry(); // undefined (no function provided)
const alsoInvalid = niceTry(42); // undefined (non-function provided)Executes an asynchronous function and silently catches any errors that occur.
/**
* Executes an asynchronous function and returns its result, suppressing any errors that occur.
*
* @param {Function} fn - An asynchronous function to execute.
* @returns {Promise<any>} The result of the function if it resolves successfully, otherwise undefined if an error is thrown.
*/
export default async function niceTry(fn);Usage Examples:
import niceTry from 'nice-try/promises';
// Safe API calls
const apiData = await niceTry(async () => {
const response = await fetch('/api/endpoint');
return response.json();
});
// Safe file operations (in Node.js)
const fileContent = await niceTry(async () => {
const fs = await import('fs/promises');
return fs.readFile('config.json', 'utf8');
});
// Safe database operations
const user = await niceTry(async () => {
return await db.user.findById(userId);
});Both variants of niceTry provide complete error suppression:
undefined when an error occursundefinedCommon scenarios where nice-try is useful: