Skip to content

Instantly share code, notes, and snippets.

@zjrosen1
Created August 30, 2016 16:40
Show Gist options
  • Save zjrosen1/a509e02b36075bc4dcee961e232b1a25 to your computer and use it in GitHub Desktop.
Save zjrosen1/a509e02b36075bc4dcee961e232b1a25 to your computer and use it in GitHub Desktop.
Batch Requests Async or Sync
var moment = require('moment');
var _ = require('lodash');
function p(i) {
return new Promise(resolve => {
setTimeout(() => {
resolve({id: i});
}, 300)
})
}
function Batch(opt) {
this.fn = opt.fn;
this.transformFn = opt.transformFn;
this.args = opt.args;
this.start = opt.start;
this.total = opt.start === 0 ? opt.total : opt.total + 1;
this.chunk = opt.chunk;
this.debug = opt.debug || false;
this.sendAsync = function() {
if (this.debug) console.log(moment().format());
var self = this;
return new Promise((resolve, reject) => {
var batchData = [];
function sendRequest() {
if (self.start >= self.total) {
if (self.debug) console.log(moment().format());
return resolve(batchData);
}
var promises = [];
for (var i = self.start; i < self.start + self.chunk; i++) {
if (i < self.total) {
self.args[0] = i;
var promise = self.fn.apply(null, self.args);
promises.push(promise);
}
}
Promise.all(promises)
.then(data => {
batchData = batchData.concat(self.transformFn(data));
if (self.debug) console.log(batchData);
self.start = self.start + self.chunk;
sendRequest();
})
.catch(reject);
}
sendRequest();
})
}
this.sendSync = function() {
if (this.debug) console.log(moment().format());
var self = this;
return new Promise((resolve, reject) => {
var batchData = [];
function sendRequest() {
if (self.start >= self.total) {
if (self.debug) console.log(moment().format());
return resolve(batchData);
}
self.args[0] = self.start;
var promise = self.fn.apply(null, self.args);
promise
.then(data => {
batchData = batchData.concat(self.transformFn(data));
if (self.debug) console.log(batchData);
self.start++;
sendRequest();
})
.catch(reject);
}
sendRequest();
})
}
}
var data = {
fulfillments: []
};
for (var i = 0; i < 50; i++) {
data.fulfillments.push({id: i});
}
function getEvenNumbers(data) {
return _.chain(data)
.filter(d => d.id % 2 === 0)
.map(d => d.id)
.value();
}
var sync = new Batch({
fn: p,
transformFn: getEvenNumbers,
args: [],
start: 0,
total: data.fulfillments.length,
chunk: 5,
debug: true
});
//sync.sendSync()
sync.sendAsync()
.then(data => {
console.log('done', data);
})
.catch(err => {
console.log('error', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment