
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
Node and Browser Compatible
View on NPM
npm install maybe-try
The module exposes 3 methods, one for synchronous operations, one for promises, and one for callbacks.
Each method takes 2 parameters, the fallbackValue for error scenarios, and a function.
Both parameters are REQUIRED, maybe-try does not supply a default value for you
Returns an object with caught errors (if any) and either the result or fallbackValue, depending on success
More in-depth examples can be found here
const maybeTry = require('maybe-try');
// Synchronous Version
const data = 'some string';
const { error, result } = maybeTry.catch(false, functionThatOperatesOnDataAsIfItWereAnArray(data));
// No need to run try/catches in your code blocks, maybeTry resolves the caught error and result with assignment
// Promise Version
function fetchReviewsForProduct(productId) {
const fallbackValue = [{ rating: 5 }];
return maybeTry.promise(fallbackValue, getReviewsPromise)
.then(({ error, result }) => {
// No need to handle catches here, maybeTry resolves both the db error and our fallback value
// Decide what you'd like to do with the error from here, either ignore and use the fallback value, or handle it manually
// However, you still have access to the error in the response body should you need it
// Access both the result and the error on response
console.log('Promise Response', result);
return result; // fallbackValue ([{ rating: 5 }])
});
}
// Callback Version
const cbFallbackValue = [{ name: 'Tom' }];
fetchFriendsForUser(1, maybeTry.callback(cbFallbackValue, (err, { error, result }) => {
// No need to handle errors here, maybeTry resolves both the db error and our fallback value
// The first argument (err) will always be null to keep with error-first callback patterns
// However, you still have access to the error in the response body should you need it
// Decide what you'd like to do with the error from here, either ignore and use the fallback value, or handle it manually
// Access both the result and the error on response
console.log('Callback Response', result);
return result; // fallbackValue (everything's ok, at least we have Tom)
}));
An optional feature to register a function to handle errors for all occurances within the module
It's useful if you'd like to only stick to the happy path with your application logic but send logs to an external service when errors do occur
It should be noted that the errorHandler is a fire and forget method. You cannot chain to it or call it explicitly.
const errorHandler = error => {
const { message } = error;
console.log('An Error was caught from maybe-try', error);
logErrorsToAnExternalService(error);
};
maybeTry.registerErrorHandler(errorHandler);
// The API remains the same, but the errorHandler will be firing in the background
const data = 'some string';
const { error, result } = maybeTry.catch(false, functionThatOperatesOnDataAsIfItWereAnArray(data));
// at this point, logErrorsToAnExternalService() has been called
FAQs
A declarative sync & async js maybe that notes something may fail
We found that maybe-try demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.