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
const fs = require('fs'); | |
const aws = require('aws-sdk'); | |
const s3Client = new aws.S3(); | |
async function uploadFilesToS3(fileNames) { | |
for (const fileName of fileNames) { | |
const fileStream = fs.createReadStream(fileName); | |
await s3Client.upload({ | |
Key: fileName, | |
Body: fileStream, |
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
// Limit of promises executed in parallel | |
// We don't want to upload more than 3 files simultaneously | |
const concurrency = 3; | |
// Returns a promise, which means uploading is fired once function is called | |
function uploadSingleFileToS3(fileName) { | |
const fileStream = fs.createReadStream(fileName); | |
return s3Client.upload({ | |
Key: fileName, | |
Body: fileStream, |
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
const crypto = require('crypto'); | |
const fs = require('fs'); | |
// Limit of files to be processed simultaneously | |
const concurrency = 3; | |
// Promisified hash computation with streams | |
function computeSingleFileHash(fileName) { | |
const hash = crypto.createHash('md5'); | |
const fileStream = fs.createReadStream(fileName); |
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
** | |
* Run Promises in parallel with a concurrency limit | |
* @param args - List of arguments to be passed to 'fn' | |
* @param fn - A function to apply to each argument. MUST return a Promise | |
* @param concurrency - Allowed amount of Promises to be run in parallel | |
* @returns {Promise} - A Promise, which eventually resolved with a list of results. | |
* Order corresponds to the list of arguments. | |
*/ | |
function mapPromises(args, fn, concurrency = 1) { | |
if (!args.length) return Promise.resolve([]); |
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
// Used to watch amount of Promises executing in a single moment of time | |
let counter = 0; | |
let interval; | |
// Overall amount of operations | |
const numberOfOperations = 25; | |
// Arguments per operation | |
const listOfArguments = []; | |
// Delays per operation to fake async request | |
const listOfDelays = []; |
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 function take0() { | |
// Harvesting | |
const results = []; | |
for (const argument of listOfArguments) { | |
const index = await asyncOperation(argument); | |
results.push(index); | |
} | |
return results; | |
} |
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 function take1() { | |
// Running Promises in parallel | |
const listOfPromises = listOfArguments.map(asyncOperation); | |
// Harvesting | |
const results = []; | |
for (const promise of listOfPromises) { | |
const index = await promise; | |
results.push(index); | |
} | |
return results; |
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 function take2() { | |
// Running Promises in parallel | |
const listOfPromises = listOfArguments.map(asyncOperation); | |
// Harvesting | |
return await Promise.all(listOfPromises); | |
} |
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 function take3subtake0() { | |
const concurrencyLimit = 5; | |
let results = []; | |
const batchesCount = Math.ceil(numberOfOperations / concurrencyLimit); | |
// Running Promises in parallel in batches | |
for (let i = 0; i < batchesCount; i++) { | |
const batchStart = i * concurrencyLimit; | |
const batchArguments = listOfArguments.slice(batchStart, batchStart + concurrencyLimit); | |
const batchPromises = batchArguments.map(asyncOperation); |
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 function take3subtake1part0() { | |
const concurrencyLimit = 5; | |
const argsCopy = listOfArguments.slice(); | |
const promises = new Array(concurrencyLimit).fill(Promise.resolve()); | |
// Recursively chain the next Promise to the currently executed Promise | |
function chainNext(p) { | |
if (argsCopy.length) { | |
const arg = argsCopy.shift(); | |
return p.then(() => { | |
const operationPromise = asyncOperation(arg); |
OlderNewer