-1

Can someone help me why my code below is returning Promise pending value for out variable?

var out = dbConn.connect().then(function (){
        var request = new sql.Request(dbConn);
        request.input("terminalId", sql.VarChar, terminalID).query("SELECT terminalContextKey from device_context where terminalId = @terminalId").then(function (result, recordSet) {
            dbConn.close();
            finalterminalkey = result.recordset[0].terminalContextKey;
            console.dir(JSON.stringify(finalterminalkey));
            Promise.resolve(recordSet);
            return JSON.stringify(finalterminalkey);


        }).catch(function (err) {
            console.log(err);
            dbConn.close();
        });

    }).catch(function (err) {
       console.log(err);
    });

    console.dir(out);
   return out;
new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34
shiva nara
  • 43
  • 8
  • This is too broad for us. "why does my code do X" questions are off-topic for Stack Overflow without research. – new Q Open Wid Aug 17 '19 at 00:28
  • @zix Normally I would not change between code formatting types, but I suppose it's because of the 6 char limit in this case. – user202729 Aug 17 '19 at 00:30
  • Anything returned from within `.then()` will result in a new promise. Assign `JSON.stringify(finalterminalkey)` to a variable in global scope instead. – Joss Classey Aug 17 '19 at 00:30
  • 1
    Possible duplicate of [Why is my asynchronous function returning Promise { } instead of a value?](https://stackoverflow.com/questions/38884522/why-is-my-asynchronous-function-returning-promise-pending-instead-of-a-val) (assuming you're not asking why it isn't a promise-resolved) – user202729 Aug 17 '19 at 00:32
  • I tried assigning to global variable, but it is returning what I assigned as. suppose null or "" – shiva nara Aug 17 '19 at 01:18
  • @shivanara: See https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call for additional explanation of why that is, but in general you just need to understand the concept of asynchronicity. – Ry- Aug 17 '19 at 01:48

3 Answers3

2

A JavaScript Promise is like a real-life promise. Let me make an example:

Person A promises Person B to get milk from the store.

Now Person A has to wait for Person B to get the milk. Person A doesn't know when the milk will be there, or maybe the milk will never arrive.

The same goes for JavaScript Promises. They will eventually resolve, but they can also throw an error. In that case the code that you put into the catch() will be used to "catch" the error. If they resolve, they will run the code in then().

Your variable out is a promise. You have to put your logic in then() or catch().

When you return out, JavaScript doesn't know if the Promise is already ready/resolved.

MarvinJWendt
  • 2,357
  • 1
  • 10
  • 36
  • I will accept the answer. almost close enough. could you point out what I am doing worng? – shiva nara Aug 17 '19 at 01:23
  • { var out = dbConn.connect().then(function (){ var request = new sql.Request(dbConn); request.input("terminalId", sql.VarChar, terminalID).query("SELECT terminalContextKey from device_context where terminalId = @terminalId") .then(function (out, err) { dbConn.close(); finalterminalkey = out.recordset[0].terminalContextKey; //console.dir(JSON.stringify(finalterminalkey)); }) }); return out; } – shiva nara Aug 17 '19 at 01:24
1

.then and .catch both return references to a new promise. This is why you can chain them. Because you are probably logging the Promise right after calling it, it hasn't resolved yet and is still pending. This is a good resource on how exactly Promises work.

0

You have to use .then and .catch statements, but they return a new promise instead of the original promise. Still, they would be good. You can check the duplicate for more references. When you use out, the promise is not resolved yet so still, if you use the .then statement which is when the promise is resolved and .catch which is when the promise fails.

new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34