Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
la variable local no se actualiza después de asignarla desde redis incluso después de usar promise.all
//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()

Después de asignar las variables locales con valores leídos de redis, se muestran vacíos cuando intento acceder a ellos más tarde. Pero si lo muestro justo después de asignar, se muestran todos los valores. He usado promesas y promesas. Todo a mano asíncrono pero aún no funciona. Por favor ayuda. Gracias.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Gracias @briosheje y @VLAZ. Aquí está el código de trabajo. He usado object y eliminé 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()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!