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

147
Views
How can i clean this code and properly use promises?

This seems really messy and even wrong but I'm not sure how to optimize it and use best practices

Also, I'm trying to better understand promises and am not sure where and how it is applicable here

const sql = require('mssql');

exports.mssql = (event, context, callback) => {

    sql.connect({
        user: 'user',
        password: 'pass',
        server: 'host',
        database: 'testdb',
        port: 1433,
    },
    err => {
        if (err) {
            console.log("Fail")
            callback('connect failed', err);
        } else {
            let sqlRequest = new sql.Request();
            let sqlQuery = "SELECT * from TestTable;"

            sqlRequest.query(sqlQuery, function(err, data) {
                if (err) {
                    console.log(err)
                }
                console.table(data.recordset);
                console.log(sqlRequest.rowCount);
                sql.close();
            });
            
            callback(null, sqlRequest);

            response = {
                statusCode: 200,
                body: JSON.stringify({
                    message: "Query returned",
                })
            }
            return response;
        }
    })
}

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