0
function event() {

    var hold = "";

    var oArgs = {
        page_size: String(pgsize)
    };

    var wow = EVDB.API.call("/events/search", oArgs, ow = function(oData) {

        hold = oData.events.event[0].description

        return console.log(hold);

    });

    $("#shower").append("<li>" + hold + "</li>");

}

Please I would like the id shower to hold the value from my console.log. It just outputs undefined presently, I know I can simply append 'hold' in 'ow' but my intent is to return 'hold' value and use it in an outer function different from event().

Austin
  • 19
  • 1
  • 8
  • That's not how asynchronous programming works. Do it in your callback, or use some form of promises, etc. – Dave Newton Jul 22 '15 at 00:00
  • Thank you! Please expanciate ...Problem is I can't really update a global variable while I'm in the execution context of that function(oData). Or is it not possible at all ? I'm a bit confused, a promise, as in angular? – Austin Jul 22 '15 at 00:18
  • Aren't you already doing that? So no, I mean you should do your jQuery work in the callback. (Also, you're currently returning the value of calling `console.log`, which seems useless.) A promise, as in anything that's a promise--they're used outside of AngularJS. – Dave Newton Jul 22 '15 at 00:19
  • Yes I can do that in the callback, I would prefer to get the value and use it in a new function totally, how do I sort of return it, is what I'm asking. cos I can see my value in my console as I want but how do I use the value – Austin Jul 22 '15 at 00:25
  • I'd consider taking a step back and wrapping your head around the async nature of calls like this, or just in general. – Dave Newton Jul 22 '15 at 00:27
  • You should remove all those `this.` things. You want to work with variables, not with properties (and also `this` is something else in every function) – Bergi Jul 22 '15 at 00:35
  • Thank you guys, I understand. – Austin Jul 22 '15 at 00:39

1 Answers1

0

In asynchronous programming, the ONLY place you can reliably use the result of the async operation is inside the callback that delivers that result. This is because the async called is called some indeterminate time in the future, meanwhile other code continues to run. Also, you cannot return a value from the async callback either.

So, you can do this:

function event() {
    var oArgs = {page_size: String(pgsize)};
    EVDB.API.call("/events/search", oArgs, function(oData)
        $("#shower").append("<li>" + oData.events.event[0].description + "</li>");
    });
}

I've also removed all references to this because there is no clear object creation in this function that would give you a proper context for this.

jfriend00
  • 683,504
  • 96
  • 985
  • 979