I have the following code that works in an Async Function, but is running Serially.
const streamPromise = stream => new Promise((res, rej) => {
stream.on( 'end', () => res('end') );
stream.on( 'finish', () => res( stream.read() ) );
stream.on( 'error', e => rej(e) );
});
for (var obj of lookupMd5) {
console.log(`Working on ${obj}`);
const hash = crypto
.createHash('md5')
.setEncoding('hex');
const readStream = s3.getObject({Bucket: bucket, Key: obj}).createReadStream();
readStream.on('error', err => {
console.error('s3 download error', err);
hash.emit('error', err);
});
readStream.pipe(hash);
const md5 = await streamPromise(hash);
console.log('md5', md5);
haveMd5.push( { Key: obj.replace(prefix, ''), Md5: md5 } );
}
Want to convert to a parallel running of the code using something like
const streamPromise = stream =>
new Promise((res, rej) => {
stream.on('end', () => res('end'));
stream.on('finish', () => res(stream.read()));
stream.on('error', e => rej(e));
});
const md5s = Promise.all(
lookupMd5.map(obj => {
console.log(`Working on ${obj}`);
const hash = crypto.createHash('md5').setEncoding('hex');
const readStream = s3.getObject({ Bucket: bucket, Key: obj }).createReadStream();
readStream.on('error', err => {
console.error('s3 download error', err);
hash.emit('error', err);
});
readStream.pipe(hash);
return { Key: obj.replace(prefix, ''), Md5: await streamPromise(hash) };
})
);
console.log('md5s', JSON.stringify(md5s, null, 2));
But Getting an Error Unexpected Token'streamPromise' in my object at Md5: await streamPromise(hash)
.
And Unexpected Token 'streamPromise' in the same place if I change to
const streamPromise = async stream => {
stream.on( 'end', () => 'end' );
stream.on('finish', () => stream.read());
stream.on('error', e => { throw e });
};
const md5s = Promise.all(
lookupMd5.map(obj => {
console.log(`Working on ${obj}`);
const hash = crypto.createHash('md5').setEncoding('hex');
const readStream = s3.getObject({ Bucket: bucket, Key: obj }).createReadStream();
readStream.on('error', err => {
console.error('s3 download error', err);
hash.emit('error', err);
});
readStream.pipe(hash);
return { Key: obj.replace(prefix, ''), Md5: await streamPromise(hash) };
})
);
console.log('md5s', JSON.stringify(md5s, null, 2));