0

I'm trying to call a function after a function finish work

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

I went to execute

 function incrementQueryValue(){
    encryptCdrVoice()
}

function after the map function done

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anik Biswas
  • 120
  • 1
  • 1
  • 7

1 Answers1

2

Try to return a promise from the callback used with map.

In this way it will be possible to wait for all promises to be resolved using Promise.all and call encryptCdrVoice after that:

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()
})
antonku
  • 7,377
  • 2
  • 15
  • 21
  • not work the map function inside encryptCdrVoice() function – Anik Biswas Jul 16 '19 at 12:07
  • If `encryptCdrVoice` is unavailable in module where map is evaluated, but `incrementQuaryValue` is, then you should call it instead: `Promise.all(tasks).then(incrementQuaryValue)` – antonku Jul 16 '19 at 12:14