I am using client.stream() from Cassandra DB's driver to fetch large result sets using pages, then for each row result returned I am pushing it into an array defined at the top of my scope.
After the queries are finished, I want to return my array but It always returns 'undefined' and I am guessing its because fetching the queries takes a long time so Javascript proceeds with the return statement before the object is even populated.
For those not familiar with this driver: The client.stream is a function, and it takes a little while to get some data. I need to wait for this to finish before returning the object!
E.g
function foo() {
  var resultArray: [];
  var query = "select username from users where userRank = 3";
  client.stream(query, {prepare: true})
    .on('readable' function () {
      var row;
      while (row = this.read()) {
        resultArray.push(row.username); 
      }
    })
    .on('end', function () {
      return obj; // The object only exists in this scope but cant return from here
    });
}
When I call this var returned = foo(); I get undefined as the return value.
 
     
    