-1

I am new to promises. I apologize I did see that similar questions have been asked here before, but it is still unclear to me how to make it work. I am trying to access value that is supposedly gets returned by the promise inside my .then() block. I need to be able access results of my SQL queries that I run inside the .then() blocks for some data validation in my testing framework.

This version perfectly works and I get result of my SQL query in the console.

const mysqlssh = require('mysql-ssh');
const fs = require('fs');

let conn = mysqlssh.connect(
    {
        host: '***',
        user: '***',
        privateKey: fs.readFileSync( process.env.HOME + '***')
    },
    {
        host: '***',
        user: '***',
        password: '***',
        database: '***'
    })

const query1 = 'SELECT count(*) as cnt FROM table';

conn.then(function(client) {
    client.query(query1, function (err, result) {
        console.log(result[0].cnt)
    })
})

With this version my console logs promise in a pending state:

const mysqlssh = require('mysql-ssh');
const fs = require('fs');

let conn = mysqlssh.connect(
    {
        host: '***',
        user: '***',
        privateKey: fs.readFileSync( process.env.HOME + '***')
    },
    {
        host: '***',
        user: '***',
        password: '***',
        database: '***'
    })

const query1 = 'SELECT count(*) as cnt FROM table';

let tableCount = conn.then(function(client) {
    client.query(query1, function (err, result) {
        return result[0].cnt
    })
})

console.log(tableCount);

When I do console.log(result[0].cnt) inside my then block - I perfectly see result in my console. However, if I try to assign it to a variable and then console log that variable I get Promise { <pending> } message in the console.

  • Effectively, you ***don't*** access it outside the asynchronous callback. Asynchronous-ness is a top-down thing. You have to stay async. – zero298 Jul 31 '19 at 18:37

3 Answers3

2

The variable tableCount is a reference to the function. You want to call it with tableCount().

console.log(tableCount());

or

let tableCount = conn.then(function(client) {
    client.query(query1, function (err, result) {
        console.log( result[0].cnt );
    })
})
Erik
  • 89
  • 4
1

Your console log appears correctly in the first example because the console log is inside the then block. This ensures that the async operation performed by conn() is completed before the console log gets reached.

In your second example, the console log appears seemingly in the middle of your code and does not ensure the asynchronous operation is completed before console logging. That's what results in the promise pending log you get when you try to console log.

You need to ensure your function completes running before try to console log the result. The first way is easier- you can also add more promise chaining or async/await further in your code to get the result and then console log after you do those additional steps.

chevybow
  • 9,959
  • 6
  • 24
  • 39
0

You are assigning a promise to the variable tableCount. That's the reason why console.log is logging a promise. To access the resolved value of the promise you can try the following:

conn
 .then((client) => {
   const promise = new Promise((resolve, reject) => {
    client.query(query1, (err, result) => {
     if(err) {
      reject(err);
     }
     resolve(result[0].cnt);
    })
   });
   return promise;
 })
 .then((tableCount) => {
   console.log(tableCount);
 })
netishix
  • 183
  • 4
  • 10