链式异步/等待无法正常工作

时间:2019-05-16 09:15:44

标签: node.js

我正在从诺言迁移到异步/等待,并且遇到了我认为很奇怪的问题。在第二个功能中,我得到的团队是不确定的。登录到控制台,我可以看到在selectTeams完成之前功能InsertAgents正在运行。可能是什么问题?

    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);

1 个答案:

答案 0 :(得分:0)

make something in this way

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

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

(async () => {
let waitfor = await awaitablePromiseFunction();
})()