mysql 在node.js中按顺序运行承诺

5rgfhyps  于 2022-12-10  发布在  Mysql
关注(0)|答案(1)|浏览(119)

我还在考虑node.js和承诺。
在我的代码中有一个区域,我希望我的SQL语句按顺序运行。
我希望在一条SQL语句完成后运行一条SQL语句。我不确定我是否正确地履行了承诺。

其中一条SQL语句:

var selectbotagentstoadd=new Promise((resolve,reject)=>{
        var sql='SELECT tbltestbotagentstoadd.AgentID FROM tbltestbotagentstoadd WHERE tbltestbotagentstoadd.IDNumber=? AND tbltestbotagentstoadd.MSISDN=?;'
        DB.query(sql,[agentidpassnum,agentnumber],function(err,results){
            if (err){
                    return reject(err);
            }; 
            return resolve(results);
        });
       })

承诺声明:

await insertbotagentstoadd.then(()=>{
   console.log("done with one");
})
.then(()=>{ selectbotagentstoadd.then((results)=>
{
        AgenttoaddIDStore=[];
       results.forEach(agent=>{
          AgenttoaddIDStore.push({
           AgentID:agent.AgentID
       });
        ctx.session.tempAgentID=agent.AgentID
       });
console.log("agent ID: "+ctx.session.tempAgentID);
console.log("done with two");
})})
.then((results)=>{insertcctblricaagents
console.log("done with three");
})
.then((results)=>{selectcctblricaagents.then((result)=>{
    console.log(result);
            AgentnewIDStore=[];
            result.forEach(agent=>{
                AgentnewIDStore.push({
                AgentID:agent.AgentID
            })
            ctx.session.AgentID=agent.AgentID
            })
            console.log("cctblricaagents agent ID: "+ ctx.session.AgentID);
            console.log("done with four");
})})
.then(insertcctblricaagentsnum.then((result)=>{
console.log("done with five");
}))
.then(selectcctblricaagentsnum.then((result)=>{
    console.log(result)
    AgentIDStore=[];
    result.forEach(agent=>{
    AgentIDStore.push({
        AgentID:agent.AgentID,
        MainNumber:agent.MainNumber,
    })
    ctx.session.AgentID=agent.AgentID
    ctx.session.agentnumber=agent.MainNumber
    })
    console.log("cctblricaagentsnum agent ID: "+ ctx.session.AgentID);
    console.log("done with six");
}))
.then(insertcctblintblbotagents.then((result)=>{
    console.log("done with seven");
}));

我从终端得到的结果:

Agent number: 27815567777
done with one
done with three
agent ID: 89
done with two
[]
cctblricaagents agent ID: null
done with four
t3psigkw

t3psigkw1#

如果正确返回每个then块中的承诺,它应该按顺序执行。
简化您的代码,例如:

await insertbotagentstoadd
  .then(() => {
    console.log("done with one");
  })
  .then(() => selectbotagentstoadd)
  .then((res) => {
    console.log("done with two");
    return res;
  })
  .then((results) => insertcctblricaagents)
  .then((res) => {
    console.log("done with three");
    return res;
  })
  .then((results) => selectcctblricaagents)
  .then((res) => {
    console.log("done with four");
    return res;
  })
  .then(() => insertcctblricaagentsnum)
  .then((res) => {
    console.log("done with five");
    return res;
  })
  .then(() => selectcctblricaagentsnum)
  .then((res) => {
    console.log("done with six");
    return res;
  })
  .then(() => insertcctblintblbotagents)
  .then((res) => {
    console.log("done with seven");
    return res;
  });

编辑:
包括你的计算在内,它会是什么样子:

await insertbotagentstoadd
  .then(() => {
    console.log("done with one");
  })
  .then(() => selectbotagentstoadd)
  .then((results) => {
    AgenttoaddIDStore = [];
    results.forEach((agent) => {
      AgenttoaddIDStore.push({
        AgentID: agent.AgentID,
      });
      ctx.session.tempAgentID = agent.AgentID;
    });
    return AgenttoaddIDStore;
  })
  .then((res) => {
    console.log("done with two");
    return res;
  })
  .then((results) => insertcctblricaagents)
  .then((res) => {
    console.log("done with three");
    return res;
  })
  .then((results) => selectcctblricaagents)
  .then((result) => {
    AgentnewIDStore = [];
    result.forEach((agent) => {
      AgentnewIDStore.push({
        AgentID: agent.AgentID,
      });
      ctx.session.AgentID = agent.AgentID;
    });
    return AgentnewIDStore;
  })
  .then((res) => {
    console.log("done with four");
    return res;
  });

相关问题