gpt4 book ai didi

javascript - 如何在异步函数完成 Node js中的工作后调用函数

转载 作者:行者123 更新时间:2023-12-01 00:50:06 24 4
gpt4 key购买 nike

我试图在函数完成工作后调用函数

 invalue.map((val) => {
const data = column.map(v=>{ return encryptData.regularDataEncrypt( val[v])});
const withcote = data.map(x => {return "'" + x + "'"})
const serialnumber_voice =val.serialnumber_voice;
const cassdataQuery=`insert into cdr_voice(id,${str},isencrypted) values(now(),${withcote.join()},false)`;
db.execute(cassdataQuery).then(data=>{
pg.query(`update cdr_voice set is_cassandra= true WHERE serialnumber_voice = ${serialnumber_voice}`).then(u=>{
console.log("is_cassandra updated",serialnumber_voice )
})
console.log("data updated ",serialnumber_voice)
})
})

我去执行

 function incrementQueryValue(){
encryptCdrVoice()
}

map 功能完成后的功能

最佳答案

尝试从与map一起使用的回调中返回一个promise。

通过这种方式,可以使用 Promise.all 等待所有 Promise 得到解决,然后调用 encryptCdrVoice:

const tasks = invalue.map((val)=>{
const data = column.map(v=>{ return encryptData.regularDataEncrypt( val[v])});
const withcote = data.map(x => {return "'" + x + "'"})
const serialnumber_voice =val.serialnumber_voice;
const cassdataQuery=`insert into cdr_voice(id,${str},isencrypted) values(now(),${withcote.join()},false)`;
return db.execute(cassdataQuery).then(data=>{
pg.query(`update cdr_voice set is_cassandra= true WHERE serialnumber_voice = ${serialnumber_voice}`).then(u=>{
console.log("is_cassandra updated",serialnumber_voice )
})
console.log("data updated ",serialnumber_voice)
})

})

Promise.all(tasks).then({
encryptCdrVoice()
})

关于javascript - 如何在异步函数完成 Node js中的工作后调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57056553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com