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

275
Vistas
How to make the program wait for the if statement to finish before continuing in javascript?

I'm new to Javascript. I want to make this block run after the if statement is finished (asynchronous). The reason I want that is that I want to make some changes to update them if it falls into the if statement

let params = {
            TableName: "storepedia-test",
            Item: updatedItem
        };
    
        docClient.put(params, function (err, data) {
            if (err) {
                console.log(err);
            } else {
                res.redirect('/devices');
            }
        });

Here is my whole code

const { id } = req.params;
const file = req.file;
let updatedItem = { ...req.body};
updatedItem.id = id;
    if (file !== undefined){
        const deleteParams = {
            Key: updatedItem.image,
            Bucket: bucketName
        }
        s3.deleteObject(deleteParams, async (err, data) => {
            if (err) {
                console.log(err)
            } else {
                const result = await uploadFile(file);
                console.log('result', result);
                await unlinkFile(file.path);
                updatedItem.image = result.Key;
                let params = {
                    TableName: "storepedia-test",
                    Item: updatedItem
                };
            
                docClient.put(params, function (err, data) {
                    if (err) {
                        console.log(err);
                    } else {
                        res.redirect('/devices');
                    }
                });
            }
        })     
    } 

        let params = {
            TableName: "storepedia-test",
            Item: updatedItem
        };
    
        docClient.put(params, function (err, data) {
            if (err) {
                console.log(err);
            } else {
                res.redirect('/devices');
            }
        });
  
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Just to run something after the if? I think this is the best spot:

docClient.put(params, function(err, data) {
  if (err) {
    console.log(err);
  } else {

    // run async code here.
    // when done do the redirect.
    // for example:
    s3.do_something(function(err, data) {
      if (err) {
        console.log(err)
      } else {
        console.log(data)
        res.redirect('/devices');
      }
    })


  }
});

about 4 years ago · Juan Pablo Isaza 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