【问题标题】:Run promises sequentially in node.js在 node.js 中顺序运行承诺
【发布时间】: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


    【解决方案1】:

    看起来你正在使用等待关键字与然后承诺的方式。这不是将 await 与 promises 一起使用的正确方法。 关键词等待只在内部使用异步功能。因为然后异步调用的方法你应该需要让你的函数成为

    喜欢:

          async function runSqlStatements() {
         // Run the first SQL statement and wait for it to 
          //complete
          await insertbotagentstoadd;
            console.log('Done with first SQL statement');
    
          // Run the second SQL statement and wait for it to 
           //complete
           const results = await selectbotagentstoadd;
         console.log('Done with second SQL statement', 
           results);
    
             // Run the third SQL statement and wait for iT 
             //to complete
           await insertcctblricaagents;
           console.log('Done with third SQL statement');
         }
    

    在您的代码中,您应该使用相同的方法按顺序运行您的 SQL 语句并等待每个语句。这将确保每条语句按顺序执行,并且您可以在代码的下一步中使用每条语句的结果。

    【讨论】:

    • 抱歉,我在 visual studio 中的代码中注释掉了其中等待的区域。我已经将它们编辑掉了,但即使在承诺中没有 await 关键字,代码仍然不会按顺序运行。你是说我应该取消承诺并坚持使用 ync……等等?
    猜你喜欢
    • 2017-01-13
    • 2020-06-16
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    相关资源
    最近更新 更多