Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

iferr

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

iferr

Higher-order functions for easier error handling

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.5M
decreased by-1.58%
Maintainers
1
Weekly downloads
 
Created

What is iferr?

The 'iferr' npm package is a utility for handling errors in Node.js callbacks. It simplifies the process of checking for errors and executing corresponding error-handling code, making the code more readable and maintainable.

What are iferr's main functionalities?

Basic Error Handling

This feature allows you to handle errors and success cases in a more readable way. The 'iferr' function takes two arguments: an error handler and a success handler. If an error occurs, the error handler is executed; otherwise, the success handler is executed.

const iferr = require('iferr');

function someAsyncFunction(callback) {
  // Simulate an asynchronous operation
  setTimeout(() => {
    const error = Math.random() > 0.5 ? new Error('Something went wrong!') : null;
    const result = error ? null : 'Success!';
    callback(error, result);
  }, 1000);
}

someAsyncFunction(iferr((err) => {
  console.error('Error:', err);
}, (result) => {
  console.log('Result:', result);
}));

Custom Error Handling

This feature allows you to define custom error and success handlers that can be reused across different asynchronous functions. This promotes code reuse and consistency in error handling.

const iferr = require('iferr');

function someAsyncFunction(callback) {
  // Simulate an asynchronous operation
  setTimeout(() => {
    const error = Math.random() > 0.5 ? new Error('Something went wrong!') : null;
    const result = error ? null : 'Success!';
    callback(error, result);
  }, 1000);
}

const customErrorHandler = iferr((err) => {
  console.error('Custom Error Handler:', err);
}, (result) => {
  console.log('Custom Success Handler:', result);
});

someAsyncFunction(customErrorHandler);

Other packages similar to iferr

Keywords

FAQs

Package last updated on 18 Jun 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc