Anyone can tell me what i’m wrong in the code below? [on hold]


As my understanding, I have tried refactor below code.

  • Do you think I should sequence promise. Is it ok if we keep using result of first response.

After refactor

const sql = require('mssql'); const connectionString = 'somecnnstring'; const query1 = 'some-query-1'; const query2 = 'some-query-2'; const query3 = 'some-query-3';  function getAccountId() {     sql.connect(connectionString).then(() => {         return sql.query(query1)     }).then(result1 => {         console.log(result1)         return sql.query(query2, {...result1})     }).then(result2 => {         console.log(result2)         return sql.query(query3, {...result2})     }).then(result3 => {         console.log(result3)     }).catch(err => {         console.log(err)     }) }  module.exports = {     getAccountId, }; 

Before

  • This is the origin code need be make better
const sql = require('mssql');    const connectionString = 'some-cnn-string';    const query1 = 'some-query-1';    const query2 = 'some-query-2';    const query3 = 'some-query-3';    module.exports = {    getAccountId: function(apiKey, cb) { sql.connect(connectionString, (connection)=>{                connection.query(query1, (result)=>{                    console.log(result1);                    connection.query(query2, { ...result1 }, (result)=>{                        console.log(result2);                        connection.query(query3, { ...result2 },                            console.log(result3);                        })    }) });    }) }    };