Last active
May 10, 2021 05:20
-
-
Save zjrosen1/b9af7943415b892ef308adf90c119cf2 to your computer and use it in GitHub Desktop.
ES6 Batch Util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Async Chunking or Sync | |
class Batch { | |
constructor(opt) { | |
this.fn = opt.fn; | |
this.transformFn = opt.transformFn || null; | |
this.args = opt.args || []; | |
this.argsIndex = opt.argsIndex || 0; | |
this.dataset = opt.dataset || null; | |
this.start = 0; | |
this.total = opt.dataset ? opt.dataset.length : opt.total; | |
this.chunk = opt.chunk || 1; | |
this.debug = opt.debug || false; | |
} | |
async sendAsync() { | |
const self = this; | |
let batchData = []; | |
async function handleBatch() { | |
if (self.start >= self.total) { | |
return batchData; | |
} | |
const promises = []; | |
for (let i = self.start; i < self.start + self.chunk; i++) { | |
if (i < self.total) { | |
self.args[self.argsIndex] = self.dataset ? self.dataset[i] : i; | |
const promise = self.fn.apply(null, self.args); | |
promises.push(promise); | |
} | |
} | |
const data = await Promise.all(promises); | |
batchData = batchData.concat(self.transformFn ? self.transformFn(data) : data); | |
if (self.debug) { | |
console.log(batchData); // eslint-disable-line | |
} | |
self.start = self.start + self.chunk; | |
return await handleBatch(); | |
} | |
return handleBatch(); | |
} | |
async sendSync() { | |
const self = this; | |
let batchData = []; | |
async function handleBatch() { | |
if (self.start >= self.total) { | |
return batchData; | |
} | |
self.args[self.argsIndex] = self.dataset ? self.dataset[self.start] : self.start; | |
const promise = self.fn.apply(null, self.args); | |
const data = await promise; | |
batchData = batchData.concat(self.transformFn ? self.transformFn(data) : data); | |
if (self.debug) { | |
console.log(batchData); // eslint-disable-line | |
} | |
self.start = self.start += 1; | |
return await handleBatch(); | |
} | |
return handleBatch(); | |
} | |
} | |
export default Batch; | |
// Example | |
// Promise | |
function p(i) { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve({id: i}); | |
}, 300) | |
}) | |
} | |
// Even Number Transform Fn | |
function getEvenNumbers(data) { | |
return _.chain(data) | |
.filter(d => d.id % 2 === 0) | |
.map(d => d.id) | |
.value(); | |
} | |
// Dataset to loop through | |
var data = { | |
fulfillments: [], | |
}; | |
for (var i = 0; i < 50; i++) { | |
data.fulfillments.push({id: i}); | |
} | |
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