M
M
mint_candy2019-05-30 23:26:20
JavaScript
mint_candy, 2019-05-30 23:26:20

How to catch synchronously data that is taken from redis?

Hello toasters.
The problem in general is this.

var data = [];  
directions.forEach((direction) => {
    var directionKey = direction.from+'-'+direction.to;
    redisClient.get(directionKey, (err, reply) => {
        if(err) next(err);
        var decoded = JSON.parse(reply);
        data.push({direction: decoded.direction, minMonthPrice: decoded.minMonthPrice.price});
    });
});
console.log(data);

In this code, I loop through the array of keys and get the values ​​​​from the radish, and push them into an array that needs to be returned to the front, and here's the problem. After the loop, my array is empty.... I understand that this is some kind of asynchrony problem, and my knowledge does not allow me to solve this problem. Tell me, how is it better to pull out the data from the radish, so that they end up in an array :D
Thanks in advance for the answers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mint_candy, 2019-05-31
@mint_candy

Once again, I found the answer to my own question.
For those who are interested, use multi in such situations, and then in exec process the array with the results.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question