Split a TypeScript Array into Chunks with a Generic Reduce Method | Dev Extent

Setup Node.js and npm package.json

{
"type": "module"
}
{
"type": "module",
"name": "splitarrayintochunks",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "tsc && node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^14.14.22"
},
"dependencies": {
"typescript": "^4.1.3"
}
}

Setup TypeScript

{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"isolatedModules": true,
"strict": true,
"module": "esnext",
"lib": ["ES2019"],
"moduleResolution": "node",
"skipLibCheck": true
},
"include": ["*.ts"],
"exclude": ["node_modules/**/*"]
}

Node.js EMFILE Error When Reading Files

import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
(async function main() {
//an array containing ten thousand undefined items
const originalArray = Array.from(Array(10000));
try {
// awaiting all ten thousand promises simultaneously
await Promise.all(
originalArray.map(async () => {
const file = await readFile("./data.json", "utf8");
console.log(file);
})
);
} catch (error) {
console.log(error);
}
})();
[Error: EMFILE: too many open files, open '/../../data.json'] {
errno: -4066,
code: 'EMFILE',
syscall: 'open',
path: '/../../data.json'
}

TypeScript Generic Reducer to Split Array Into Chunks

const chunkItems = <T>(items: T[]) =>
items.reduce((chunks: T[][], item: T, index) => {
const chunk = Math.floor(index / 512);
chunks[chunk] = ([] as T[]).concat(chunks[chunk] || [], item);
return chunks;
}, []);
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const chunkItems = <T>(items: T[]) =>
items.reduce((chunks: T[][], item: T, index) => {
const chunk = Math.floor(index / 512);
chunks[chunk] = ([] as T[]).concat(chunks[chunk] || [], item);
return chunks;
}, []);
(async function main() {
const originalArray = Array.from(Array(10000));
const chunks = chunkItems(originalArray);
try {
for (const chunk of chunks)
await Promise.all(
chunk.map(async (item, index) => {
const file = await readFile("./data.json", "utf8");
console.log("-----start item------");
console.log("current array chunk:" + chunks.indexOf(chunk));
console.log("file contents: " + file);
console.log("current item index: " + index);
console.log("-----end item-------");
})
);
} catch (error) {
console.log(error);
}
})();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store