0

Have the following called library:

var rd = require('redis.dump')

used in function to query the database and do some processing and then return

function query(type, row, column){
  var output = []
  ...
  rd({
      filter: ...
      port  : ...
      format:  ...
      },
      function(err, result){
       [where the processing of result begins]
       ...
       output = [processed result]
       ...
      }
     });
  return output;
  }

How to out async in place to wait until rd finishes and then return output? I tried the following and failed:

function query(type, row, column){
  var output = []
  ...
  rd({
      filter: ...
      port  : ...
      format:  ...
      },
      async function convert(err, result){
       [where the processing of result begins]
       ...
       output = [processed result]
       ...
      }
     });
  rd.convert.then(return output);
  }   

with a TypeError:

TypeError: Cannot read property 'then' of undefined

Thanks in advance

ctlkkc
  • 57
  • 8
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – CRice Apr 27 '18 at 00:02
  • `rd.convert` doesn’t make sense. `rd` is a function, not an object with a `convert` property. – Sebastian Simon Apr 27 '18 at 00:04

1 Answers1

0
// replace your code into this 
function query(type, row, column,cb){
  var output = []
  ...
  rd({
      filter: ...
      port  : ...
      format:  ...
      },
      function(err, result){
       [where the processing of result begins]
       ...
       output = [processed result]
       // call callback function and pass output result
       cb(output)
      }
     });
  }

// to call this function write

query(type, row, column,function(output){
  console.log(output)

})
Ahmed Kesha
  • 810
  • 5
  • 11