Mi código casi funciona bien, mi problema es que solo se registran los primeros datos CSV y finaliza la función lambda.
Supongo que de alguna manera necesito esperar a que terminen todas las stream pipes .
myCsvList.forEach((myElem) => { const data = []; // setup params const csvFile = s3.getObject(params).createReadStream(); // works fine csvFile .pipe(csv()) .on('data', function(entry) { data.push(entry); }) .on('end', () => { console.log(data); // after the log of the first element on myCsvList, the code finishes. It should log all csvFiles from myCsvList }); });¿Supongo que necesito una promesa o algo así?
puede cambiar cada elemento a Promise, luego use Promise.all() :
const getItem = (myElem) => { const data = []; // setup params const csvFile = s3.getObject(params).createReadStream(); // works fine return new Promise((resolve) => { csvFile .pipe(csv()) .on('data', function (entry) { data.push(entry); }) .on('end', () => { resolve(data); // after the log of the first element on myCsvList, the code finishes. It should log all csvFiles from myCsvList }); }); }; const start = () => { // all promises have been finished return Promise.all(myCsvList.map(myElem => getItem(myElem))); } myCsvList.map(myElem => getItem(myElem)) puede obtener una lista de promesas, cuando todas las promesas se hayan resuelto, activará Promise.all().