0

How to get below return value?

var data = sql.execute({query:"select * from Profile_User"}).then(function (result){    
         return result; // actual result = [{id:1, name:"Mr. Test"}] 
        }, function( err ) {
        console.log( "Something bad happened:", err );
});

console.log("Received data"+JSON.stringify(data));

Result in my console : Received data{"_handler":{"resolved":false}}

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
azim hamdan
  • 113
  • 10

3 Answers3

3

As it already answered above - your function returns Promise, so result will be defined only when promise will be resolved (or will not be defened if promise will be failed).

The best practice here is build your async program by the way, when all code, which requires result will be placed on result/reject Promise callbacks:

var data = sql.execute({query:"select * from Profile_User"}).then(function (result){    
         console.log("Received data"+JSON.stringify(data));
         // doing anything with data
        }, function( err ) {
        console.log( "Something bad happened:", err );
});

If you need to "wait" while promise will be resolved - you can use new async/await feature, but you may understand, thit this stuff will stop your thread execution until promise resolution.

In that case you need to wrap your async code to another async function, like this:

async executeSQLAndDoSomething () => {
    try {
        var data = await sql.execute({query:"select * from Profile_User"}) // Will be thread stop here
        console.log(data)
        // Doint something with data
    } catch (err) {
        // ... error checks 
    }
}

Read more info about async/await here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

MobDev
  • 1,614
  • 17
  • 17
0

You can't return a value from a Promise. All logic you need to put on the rejected or fulfilled functions.

var data = sql.execute({query:"select * from Profile_User"}).then(function (result){    
               console.log("Received data" + JSON.stringify(result)); 
           }, function( err ) {
               console.log( "Something bad happened:", err );
           });
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

sql.execute is an async call, console.log() won't wait for it to finish and would print the current promise and its state
to access the result you can do

var data = sql.execute({query:"select * from Profile_User"}).then(function (result){    
         return result; // actual result = [{id:1, name:"Mr. Test"}] 
        }, function( err ) {
        console.log( "Something bad happened:", err );
});

data.then(function(result){
    console.log("Received data"+JSON.stringify(data));    
})
marvel308
  • 10,288
  • 1
  • 21
  • 32
  • but, how if I want to get the result value and put as new variable, so that I can make use the value for the any activity. – azim hamdan Aug 20 '17 at 09:58
  • You can do it inside the then, but you can't access it synchronously, you would have to access it in a then() if you want to use it after promise is resolved – marvel308 Aug 20 '17 at 10:02
  • can I return the value after the promise is resolved and assign to new variable? I want to get the value outside the promise – azim hamdan Aug 20 '17 at 10:06
  • The value would be set inside the then but if you want to use the value once the promise is resolved you would have to wait for promise.resolve(), best way to do that is in a then – marvel308 Aug 20 '17 at 10:18
  • Actually I want to put the value inside new variable example `var a = [{result}]` then `module.exports = a` – azim hamdan Aug 20 '17 at 10:26
  • you won't be able to do that, you would have to export a promise – marvel308 Aug 20 '17 at 10:30
  • here is the hint: [link](https://stackoverflow.com/questions/32774491/is-providing-a-promise-as-a-modules-export-a-valid-pattern-for-asynch-initializ) – azim hamdan Aug 20 '17 at 11:54
  • Yes, that link does what you want using promises – marvel308 Aug 20 '17 at 11:57