【发布时间】:2022-12-11 08:56:39
【问题描述】:
我仍在研究 node.js 并做出承诺。
在我的代码中有一个区域,我希望我的 sql 语句按顺序运行。
我希望在一条 sql 语句完成后运行一条 sql 语句。我不确定我是否正确地执行了承诺。
承诺声明:
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");
return result;
}))
.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");
return result;
}));
我从终端得到的结果:
Agent number: 27815567777
done with one
done with three
agent ID: 89
done with two
[]
cctblricaagents agent ID: null
done with four
【问题讨论】:
标签: mysql node.js promise sequential