Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created December 22, 2021 11:03
const processDataArray = (dataArray, finishCallback) => {
// take a new copy of array
const todo = dataArray.concat();
// to store each processed data
let result = [];
// timer function
const timedProcessing = () => {
const start = +new Date();
do {
// process each data item and store it's result
const singleResult = processSingleData(todo.shift());
result.push(singleResult);
// check if todo has something to process and the time difference must not be greater than 50
} while (todo.length > 0 && +new Date() - start < 50);
// check for remaining items to process
if (todo.length > 0) {
setTimeout(timedProcessing, 25);
} else {
// finished with all the items, initiate finish callback
finishCallback(result);
}
};
setTimeout(timedProcessing, 25);
};
const processSingleData = data => {
// process data
return processedData;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment