Express js - async pending

358 Views Asked by At

i'm trying return data to view from multiple selects and i'm using the async.parallel but in console show me Promise {<pending>} . I require the var async = require('async');

This is the first time that i try use it, what i'm doing wrong?

 new(req, res)
  {
    async.parallel(
    {
      one: function(callback) {

        callback(null, request.query("SELECT * FROM table1 where ref like '90%'"));
      },
      two: function(callback) {

        callback(null, request.query("SELECT * FROM table2 where ref like 'K%'"));

      }
    }, function(err, results)
    {
      console.log("RESULT:");
      console.log(err);

      return res.render('view-1', {"one": results.one, "two": results.two});
    });
  }

Thank you

1

There are 1 best solutions below

0
On

Shot in the dark: Try to change from callbacks to Promises

var request1 = new request.query("SELECT * FROM table1 where ref like '90%'");
var request2 = new request.query("SELECT * FROM table2 where ref like 'K%'");

return Promise.all([request1, request2]).then(function(listOfResults) {
    return res.render(...);
}).catch(function(err) {
    // ... query error checks 
});

The Promise.all's then will only be fired if both queries are successfull and the list of the results will be in the same order as the queries.

If you're not using ES6 then you can import Promise like this

var Promise = require('bluebird');