You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

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


Version published
Weekly downloads
4.2M
decreased by-18.23%
Maintainers
1
Install size
4.11 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

iferr

Higher-order functions for easier error handling.

if (err) return cb(err); be gone!

Install

npm install iferr

Use

JavaScript/ES6 example

var iferr = require('iferr');

function get_friends_count(id, cb) {
  User.load_user(id, iferr(cb, user =>
    user.load_friends(iferr(cb, friends =>
      cb(null, friends.length)
    ))
  ))
}

JavaScript/ES5 example

var iferr = require('iferr');

function get_friends_count(id, cb) {
  User.load_user(id, iferr(cb, function(user) {
    user.load_friends(iferr(cb, function(friends) {
      cb(null, friends.length)
    }))
  }))
}

CoffeeScript example

iferr = require 'iferr'

get_friends_count = (id, cb) ->
  User.load_user id, iferr cb, (user) ->
    user.load_friends iferr cb, (friends) ->
      cb null, friends.length

(TODO: document tiferr, throwerr and printerr)

License

MIT

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc