setting a variable to get return from call back function using promise

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

Leave a Comment