Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!