npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reduce-future

v0.1.2

Published

Reduce boils down a list of values into a single values. With reduce-future, you will able to reduce one array list that need execute async functions, joining the results on success callback.

Downloads

18

Readme

reduce-future

Why?

Reduce boils down a list of values into a single values. With reduce-future, you will able to reduce one list that need execute async functions, joining the results on success callback.

Installation

Via npm:

npm install reduce-future

Features

* Reduce array list of objects, executing async operations
* Monolithic and Parallel methods
* Timeout support.
* Immediate finished when error occur.
* Unified error handling.
* No magic on finished callback, just one done function.

Test on node

$ git clone [email protected]:pflima92/reduce-future.git
$ cd reduce-future && npm install

Example

var _rf = require('reduce-future');

function getPersonService(id, cb) {

	// If id generated is GREATER that 5000 call err callback
	if(id > 5000){

		cb('has id > 5000', null);
		return;
	}

	// Execute callback of success
	cb(null, {
		id : id,
		name : 'Person ' + id
	});
}


//Generate random id's
var ids = [];
for (var i = 0; i < 10; i++) {

	ids.push(Math.floor(Math.random() * 1000) + 1);
}

// Create CompletableFuture instance
// When call parallel() indicates that alghoritm is a parallel process
// When call reduce() indicates that alghoritm is a monolitich process
_rf.parallel()
	.addAll(ids) //Add all ids to be processed
	.next(function(el, context, next, fail) {
		
		//Define the each iteration flow
		
		//Call external services
		getPersonService(el, function(err, resp) {
			
			if(err){
				
				//Indicates that reduce flow should be stopped
				fail(err);
			}
			
			//Set the context populating response
			context[el] = resp;
			
			//Notify CompletableFuture that this call has executed with success
			next(context);
		});
	}).error(function(error) {
		
		//Callback of error

		// if any getPersonService call fail will show: ==> has id > 500 message.
		console.error('fail: '  + error); 
		
	}).done(function(context) {
	
		//Success callback
		
		console.info(context);
		
		 /* ==> { '30': { id: 30, name: 'Person 30' },
			  '38': { id: 38, name: 'Person 38' },
			  '42': { id: 42, name: 'Person 42' },
			  '43': { id: 43, name: 'Person 43' },
			  '48': { id: 48, name: 'Person 48' },
			  '64': { id: 64, name: 'Person 64' },
			  '70': { id: 70, name: 'Person 70' },
			  '71': { id: 71, name: 'Person 71' },
			  '73': { id: 73, name: 'Person 73' },
			  '85': { id: 85, name: 'Person 85' } }*/
	});

API

reduce

Create new CompletabeFuture class with default options.

parallel

Create new CompletabeFuture class with parallel option marked with true.

CompletableFuture

Class (ES6) responsible for manage reduce future features.

.add(Object)

Add element to CompletableFuture, can be called any times that is needed, return your own instance.

.addAll(Array)

Add array and concat at elements of CompletableFuture, can be called any times that is needed, return your own instance.

.next(Function)

Add function that will be called on each iteration of CompletableFuture execution

The callback of next future need be signed like:


function(el, context, next, fail){

}

.fail(Function)

Add error callback. Is called when next function call fail() method.

.done(Function)

Function that receive the callback parameter, responsible for join the results of interration

License

MIT