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);

2 Answers 2

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
6
  • Actually the function that gets applied does not need to open the box itself, it gets presented the value alone.
    – Bergi
    Commented Mar 21, 2014 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 Commented Mar 28, 2014 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
    Commented Mar 28, 2014 at 13:46
  • Ah, then the images are ambiguous - the open box was supposed to represent the value itself being accessible. :) Commented Mar 28, 2014 at 14:07
  • Can this also be accomplished by doing console.log(await loadDB(2)) and is it practicable?
    – Brentspine
    Commented Nov 10, 2022 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);
});
2
  • @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 :) Commented Mar 20, 2014 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. Commented Mar 20, 2014 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.