-1

I'm migrating from promises to async/await and I'm having what I think is a strange issue. In the second function I'm getting that teams is undefined. Logging to the console I can see that the function InsertAgents is running before selectTeams finishes. What could be the issue?

    let selectTeams = function (){

                const request = new mssql.Request(pool);

                let query = `SELECT * from teams`;

                request.query(query, (err, result) => {

                    if (err === null) {
                        return result.recordset;

                    } else {
                        utils.logger.error(err);
                        reject(err);
                        console.log(err);
                    }

                })

        };

let insertAgents = function (params) {    
    console.log(params);
};

        const teams = await selectTeams();
        const agents = await insertAgents(teams);
Carlos Alves Jorge
  • 1,919
  • 1
  • 13
  • 29

1 Answers1

0

make something in this way

function awaitablePromiseFunction(){
   return new Promise((resolve, reject) => {

     asyncCallback = (response) => resolve(response)
   }
}

(async () => {
let waitfor = await awaitablePromiseFunction();
})()
Estradiaz
  • 3,483
  • 1
  • 11
  • 22