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
async
The 'async' package provides a collection of functions for working with asynchronous JavaScript. It includes utilities for handling errors in asynchronous operations, such as 'async.waterfall' and 'async.series'. Compared to 'iferr', 'async' offers a broader range of functionalities for managing asynchronous control flow.
promise
The 'promise' package is a lightweight implementation of Promises/A+ for Node.js and browsers. It allows for more modern and flexible error handling using promises and async/await syntax. While 'iferr' focuses on callback-based error handling, 'promise' is more suitable for applications that use promises.
bluebird
The 'bluebird' package is a fully-featured promise library for JavaScript. It provides advanced features like cancellation, iteration, and error handling. 'bluebird' is more powerful and versatile compared to 'iferr', which is specifically designed for simplifying callback-based error handling.
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