Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
Effective way to wait until file upload has finished (Mongoose)

I'm currently developing an application with React and Mongoose (MongoDB) where I upload a text file, that contains roughly around 9000+ lines, containing JSON. Each line is important and I need to be able to store it in the database to be accessible later.

I have set up two functions, one that reads the file and one that creates a model for each request and saves it to MongoDB.

Function to read each line

const createAndReadFile = async (filePath) => {
    //create a read stream of the File
    let fileStream = fs.createReadStream(filePath, 'utf8');

    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });

    for await (const line of rl) {
        let parsedJSONLine = JSON.parse(line);
            createAndAddToDB(parsedJSONLine);
    }
};

Function to add to the database each line

const createAndAddToDB = async (jsonRequest) => {
    try {
        let request = new Request(jsonRequest);

        await request.save();
    } catch (error) {}
};

My question is, what's the most effective way to wait until all the lines have been converted to a model and saved in the database ?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Use Promise.all to await the resolution of all promises that are created in the for-await loop:

var promises = [];
for await (const line of rl) {
  let parsedJSONLine = JSON.parse(line);
  promises.push(createAndAddToDB(parsedJSONLine));
}
return Promise.all(promises);

Alternatively, you can perform the conversion in the for-await loop and write to the database in a batch:

var promises = [];
for await (const line of rl) {
  let parsedJSONLine = JSON.parse(line);
  promises.push(convertToDBFormat(parsedJSONLine));
}
return Promise.all(promises).then(function(converted) {
  return addToDBBatch(converted);
});
over 4 years ago · Santiago Trujillo Denunciar

0

const createAndReadFile = async (filePath) => {
    //create a read stream of the File
    let fileStream = fs.createReadStream(filePath, 'utf8');

    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });

    for (const line of rl) {
        let parsedJSONLine = JSON.parse(line);
            await createAndAddToDB(parsedJSONLine);
    }
    
    return 'file is processed';
};

You have written it correctly, I guess. When the loop is over, it will be the last line of the file.

Now, if there are any lines that are not saved you can push them over to the failed array maybe and save them in the batches.

Suggestion: also, instead of saving every line to DB every time that's a lot of requests 9000+ (lines), you should use batch insert. When you are out of the loop, you can again check if there are lines that were not saved. Re-run the batch save function and you are good to go.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda