Search code examples
node.jstypescriptnode-mssql

Control moving to the next `then` clause immediately


I am trying to learn Typescript and trying to use functional programming. The program is very simple, it queries MongoDB, for each document returned, it calls SQL Server and retrieves some data. The code to retrieve data from SQL Server is -

sql.ts

const dbConfig: sql.config = {
    driver: "msnodesqlv8",
    connectionString: 'Driver={ODBC Driver 17 for SQL Server};Server=server.com;Database=dbName;Trusted_Connection=yes;',
    options: {
        trustedConnection: true,
        encrypt: true,
        trustServerCertificate: true,
        
    }
}
async function sqlConnect(config: sql.config):Promise<sql.ConnectionPool>{
    let pool =  await sql.connect(config)
    return pool
}
async function createPreparedStatement(conn:sql.ConnectionPool,query, inputParams:string[]):Promise<sql.PreparedStatement>{
    const ps = await new sql.PreparedStatement(conn);
    ps.input('id',sql.NVarChar)
    await ps.prepare(query)
    return ps
}

async function executePreparedStatement(inputParams, ps: sql.PreparedStatement){
    const result = await ps.execute(inputParams)
    return result
}

the code to extract data from SQL Server using sql.ts is in the function -

async function checkMarketplaceStatus(item){

    let systemAssign = item["Id"]
    const connection:sql.ConnectionPool = await sqlConnect(dbConfig)
    const ps: sql.PreparedStatement = await createPreparedStatement(connection,
        "select Column_Name from Table_Name where Id = @id",
        ['id'])
    const data = await executePreparedStatement({id: systemAssign},ps)

    if (ps){
        await ps.unprepare();
    }
    return {mongoData: item, sqlData:data.recordset[0]}
}

The main logic to extract data from Mongo and then call SQL Server logic is -

...code to fetch document from mongo
}).then((data)=>{
       
    const promises = data.map( (item)=>{
         return checkMarketplaceStatus(item)   
    })
        
    return Promise.allSettled(promises)
        
}).then((data)=>{
    console.log(data) //-----data is undefined, NOT EXPECTED
})

checkMarketplaceStatus should be resolved before moving to then clause. I am expecting that, but it's not happening, When I inserted console.log, in function checkMarketplaceStatus, after calling sqlConnect, the logic is not waiting for PreparedStatement and execute, it's moving to then clause. If I have to comment, it seems like since it is waiting for database connection (somehow it is async even though I have used await)

I tried using console.log and checked the documentation for node-mssql but I am not able to find the issue.

Please note, the logic to extract data from SQL Server is working fine. It is extracting expected data, however because of sequencing of execution, the overall logic is failing.


Solution

  • Sometimes all it takes is to take a break, be away and then come back again. The solution is in the github issue discussed here - https://github.com/tediousjs/node-mssql/issues/517