0

I have promisified the code of caching dataand pushing the array of Json values in the new array but after the chaining, array is showing undefined. Here is the code snippet.

'use strict';
const Promise = require('bluebird');
let _connectResolve, _connectReject, onConnected = new Promise((resolve, reject) => {
        _connectResolve = resolve;
        _connectReject = reject;
    }),
    redis = require("redis"),
    redisClient = redis.createClient({
        host: 'localhost',
        port: 6379
    });
Promise.promisifyAll(redis.RedisClient.prototype);

redisClient.on('connect', _connectResolve);
const results = Promise.all([
    'iems/0I0g2I92u0U3k/120s.zip',
    'ims/25213u0X462g/10es.zip',
    'ims/2x0n440V1A0f1K/F.zip',
    'its/2l0239311f1u0w1S2a3j/Files.zip',
    'its/2O2x212i0a2f1j3t0B2h/Files.zip',
]);
onConnected.then(() => {
      var message = [];
      results.map(function(result) {
          redisClient.getAsync(result).then((reply) => {
              return new Promise((resolve, reject) => {
                  resolve({
                      'response': reply,
                      'req': result
                  });
              });
          }).then(function(reply) {
              if (reply['response'] != true) {
                  message.push({
                      "key": reply['req'],
                      "bucket_name": 'string here'
                  });
              }
          });
    }).then(function(){console.log(message)});
});

Conclusion -> message is undefined

0

1 Answer 1

1

message is not in scope. A quick fix would be to declare message outside of your promise chain (directly under const results = ...).

There are also some other funky things going on with this code - map isn't really mapping anything and some of your thens don't return a value.

Sign up to request clarification or add additional context in comments.

1 Comment

Sure, I have made some changes but still not getting the entire array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.