0
//local variables
let b1 = [] 
let b2 = []
let b3 = []

async function start(){
    //inserting some data to redis keys a1, a2 and a3
    let data = []
    for(i=1; i<=3; i++){
        data.push(i)
        client.set('a'+i, JSON.stringify(data))
    }

    //now in redis we have a1->[1] , a2->[1,2] and a3->[1,2,3]

    //array for promise.all
    let arrayX = []

    for(i=1; i<=3; i++){
        arrayX.push(y(i).then(result => {
            eval('b'+i + '=' + result) //updating the value from redis into local variables b1, b2 and b3
            console.log(eval('b'+ i)) //here the values of b1, b2 and b3 are displayed
        }))
    }
    //waiting to update b1, b2, b3
    await Promise.all(arrayX).then(()=>{
        for(i=1; i<=3; i++){
            console.log(eval('b'+ i)) //here it is displaying empty array
        }
        //here I want to call another function where I want to use the values of b1, b2 and b3
    })
}
//reading from redis and returning value
function y(i, result){
    return new Promise((resolve, reject) => {
        client.get('a'+i, async function(err,result){
            resolve(result)
        })
    })
}

start()

After assigning the local variables with values read from redis, they are showing empty when I try to access it later. But if I display it right after I assign all the values are displayed. I have used promises and promise.all to hand asynchronous but it still doesn't work. Please help. Thanks.

  • 1
    I would strongly suggest not using `eval` at all. See ["Variable" variables in JavaScript](https://stackoverflow.com/q/5187530) - if you need a lot of variables that can be resolved programmatically, use an array or an object instead. – VLAZ Nov 26 '21 at 12:56
  • Avoid using global variables in this way, this software will most likely fail in case multiple processes run the same code with the same redis database instance. Get what you need from redis, and use the values in a function through parameters, avoid using globals when unnecessary. If you need to pass an unknown amount of variables to a function, use the proper type (an array, an object or whatever). – briosheje Nov 26 '21 at 13:01
  • @briosheje I have used object `let js = {} for(i=1; i<=3; i++){ arrayX.push(y(i).then(result => { js[i] = result console.log(js[i]) })) } await Promise.all(arrayX).then(()=>{ console.log(js) })` Here is the output [1] [1,2] [1,2,3] { '4': '[1,2,3]' } which should have been [1] [1,2] [1,2,3] { '1' : '[1]', '2' : '[1,2]', '3' : '[1,2,3]' } also the 'i' got incremented to 4 – Pranjal Pratim Hazarika Nov 26 '21 at 13:13

1 Answers1

0

Thanks @briosheje and @VLAZ. Here is the working code. I have used object and removed eval.

//local variable
let x = {}

async function start(){
    //inserting some data to redis keys a1, a2 and a3
    let data = []
    for(i=1; i<=3; i++){
        data.push(i)
        client.set('a'+i, JSON.stringify(data))
        client.expire('a'+i, 3600);
    }

    //now in redis we have a1->[1] , a2->[1,2] and a3->[1,2,3]

    //array for promise.all
    let arrayX = []

    for(i=1; i<=3; i++){
        arrayX.push(y(i))
    }
    //waiting to update x
    await Promise.all(arrayX).then(()=>{
        for(i=1; i<=3; i++){
            console.log(x['b'+ i]) //here it is displaying all the values
        }
    })
}
//reading from redis and updating value in local variable
function y(i, result){
    return new Promise((resolve, reject) => {
        client.get('a'+i, async function(err,result){
            x['b'+i] = result
            resolve(result)
        })
    })
}

start()