11

I am getting the "object" value instead of the exact value. How do I get the value returned using a callback function?

function loadDB(option, callBack){
    var dfd = new jQuery.Deferred(),
        db = window.openDatabase('mydb', '1.0', 'Test DB', 1024*1024),
        selectQuery = "SELECT log FROM LOGS WHERE id = ?";
    db.transaction(function(tx){
        tx.executeSql(selectQuery,[option],function(tx,results){
            var retval;
            if( results.rows.length ) {
                retval = unescape(results.rows.item(0)['log']);
            }
            var returnValue = dfd.resolve(retval);
        });
    });
    return dfd.promise();
}
results = loadDB(2).then(function(val){ return val; } );
console.log("response***",results);
Alexis Tyler
  • 1,394
  • 6
  • 30
  • 48
Ananthan
  • 111
  • 1
  • 1
  • 3

2 Answers2

43

A promise is like a closed box:

enter image description here

Your above code with the deferred object, creates the box, and lets you know that some time in the future you can open it. That time is when that above code will call .resolve.

When you do results = loadDB(2) you are putting a box in results.

A promise also has a method that opens the box, works on the value and returns another box on the value (also opening any additional boxes along the way). That method is .then:

In boxes, it does:

enter image description here =>( open. => e) => e

That is, it takes the box, opens it and applies a function that does something to the value in it and then returns another box with the new value on it.

So, if you want to process the value, you need to hook on the one place where the box is open, like Bergi suggested:

loadDB(2).then(function(val){
    console.log("response***", val);
}); // this also returns a promise btw
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Actually the function that gets applied does not need to open the box itself, it gets presented the value alone. – Bergi Mar 21 '14 at 14:57
  • That's what the above notation says? It's like a monad in that sense - it takes a box (the this) and a function that takes an open box and returns a closed box - and returns a closed box on the second value - more formally (disregarding recursive assimilation) (M t)→(t→M u)→(M u) @Bergi – Benjamin Gruenbaum Mar 28 '14 at 13:26
  • Yup, but the sentence below the image doesn't :-) Also, I'd rather make it "content of the box" instead of "open box", that would sound like `M t -> (OpenM t -> M u) -> M u` – Bergi Mar 28 '14 at 13:46
  • Ah, then the images are ambiguous - the open box was supposed to represent the value itself being accessible. :) – Benjamin Gruenbaum Mar 28 '14 at 14:07
  • Can this also be accomplished by doing `console.log(await loadDB(2))` and is it practicable? – Brentspine Nov 10 '22 at 19:46
8

You cannot get the resolve value out of a promise (here: asynchronous) context.

Instead, you will need to move the console.log call and everything else that depends on it into the promise context:

loadDB(2).then(function(val){
    console.log("response***", val);
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @dystroy it's "how does asynchronous concurrency work in JavaScript" - these questions are common and they'll stay common. It's a fair question and Bergi's answer is good :) – Benjamin Gruenbaum Mar 20 '14 at 14:50
  • @BenjaminGruenbaum of course it's a good answer. I just wonder if we should or not link to the big canonical QA. – Denys Séguret Mar 20 '14 at 14:50