我有以下代码可在异步功能中使用,但是正在串行运行。
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 } );
}
想使用类似的东西转换为并行运行的代码
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));
但是在Md5: await streamPromise(hash)
的对象中得到了意外的令牌'streamPromise'。
如果我更改为
,则在同一位置出现意外令牌'streamPromise'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));