Like forEachAsync, or Promise.all(), but handling a bounded number of items at any given time.
Go to file
AJ ONeal 0c2692f9fb bump v1.0.1 2019-06-13 17:47:44 -06:00
.gitignore Initial commit 2019-06-13 23:04:48 +00:00
LICENSE Initial commit 2019-06-13 23:04:48 +00:00
README.md update README 2019-06-13 17:47:27 -06:00
batchasync.js for batching async tasks 2019-06-13 17:36:25 -06:00
package.json bump v1.0.1 2019-06-13 17:47:44 -06:00
test.js for batching async tasks 2019-06-13 17:36:25 -06:00

README.md

batchasync.js

Like calling map() and resolving with Promise.all(), but handling a bounded number of items at any given time.

Install

npm install --save batchasync

Usage

// Browsers
var batchAsync = window.batchAsync;
// Node
var batchAsync = require('batchasync').batchAsync;
var batchSize = 4;
var things = ['apples', 'bananas', 'pears'];

function doStuff() {
	// ... go fetch things
}

batchAsync(batchSize, things, doStuff).then(function(results) {
	// all results, in order, just like Promise.all()
	console.log(results[0]);
});

Returning 'undefined'

Returning undefined will cause an exception to be thrown (because it looks like a skipped promise). Return null instead.

Example (bad):

function doStuff(thing) {
	// Oops! forgot to return
	request('https://searchallthe.pictures/api/' + thing);
}

Example (good):

function doStuff(thing) {
	return request('https://searchallthe.pictures/api/' + thing);
}

Example (good):

function doStuff(thing) {
	return null;
}