Coder Social home page Coder Social logo

nofail's Introduction

nofail

Failover version builder for fn(array). Promise-based.

Installation

npm i nofail --save

Usage

var nofail = require('nofail');

// assume it is remote endpoint implementation, which is called using HTTP request
function greet (names) {
	// this is 'validation'
	if (_.contains(names, 'Bob')) {
		throw new Error('Sorry Bob!');
	}
	// this is 'batch processing'
	return names.map(function (name) {
		return 'Hi ' + name;
	});
}

function failureHandler (error, item) {
	console.log(item, 'is causing', error.toString(), 'error');
}

var greetFailover = nofail(greet, failureHandler);

greetFailover(['Melissa', 'Bob', 'Jess', 'Peter']).then(console.log)

// Bob is causing Sorry Bob! error
// ['Hi Melissa', 'Hi Jess', 'Hi Peter']

Also, you can pass any number of additional arguments (after array argument). They will be passed to fn on each call.

greetFailover(['Me', 'You'], 'custom1', 12, { any: 'data' }).then(console.log);

Motivation

Assume you are sending batch of items to certain web service (in a single request) and whole call fails. And the reason is that some of items do not pass remote validation. But you still want to make correct items to be accepted, so you are trying failover solution, which will try to send them, dividing initial batch into smaller ones until everything but erroneous is accepted by remote server.

Mechanism

In case of failure, array is divided into two equisized (if possible) ones, then two function calls are made (each on corresponding subarray). If array is not dividable (length is less than 2), then failure handler is called providing an error and failover process ends. In the end, results are combined into single array to make same output as you would expect from initial function. P. S. Failure handler can be omitted (if you do not want to process failures).

License

MIT

nofail's People

Contributors

titarenko avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.