Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda