Files
  • index.js
  • package-lock.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const { chunk } = require('lodash');

const BATCH_SIZE = 20;

const allFiles = Array.from(Array(200)).map((_, i) => `file-${i + 1}`);

async function upload(files) {
	const uploadingPromises = files.map(file => {
		const url = `https://myuploadurl.com/${file}`;
		return new Promise(resolve => {
			setTimeout(() => {
				console.log(`Uploaded file: ${url}`);
				resolve();
			}, 3000);
		});
	});
	return Promise.all(uploadingPromises);
}

async function batchUploads(files) {
	console.info(`Staring batching in groups of ${BATCH_SIZE}`);
	const chunks = chunk(files, BATCH_SIZE);

	for (let c of chunks) {
		await upload(c);
	}
}

batchUploads(allFiles)
	.then(() => {
		console.info('Finished iterating');
	})
	.catch(err => {
		console.error('Error:', err);
	});
node v9.7.1 linux/amd64