how can I return values from chained deferreds in jQuery? -
i'm trying trace return value function call:
$('#button').on('click', function(){ console.log( getmessage(3) ); // i'm trying "hang" until ajax-related stuff finished below });
the ajaxfetch()
below generic ajax handler returns expected ajax deferred object. let's assume it's string value: 'hello'
. server response several seconds.
function getmessage(id){ ajaxfetch(id).done(function(result){ // ... more stuff happening, not relevant }).then(function(result){ return (result); // thought return click handler }); }
how can trace output 'hello'
?
i think...
... console.log()
needs somehow set promise
i'm having hard time understanding jquery documentation.
return promise interface , code logic it:
$('#button').on('click', function(){ $.when(getmessage(3)).then(function(result){console.log(result)}); }); function getmessage(id){ return ajaxfetch(id).done(function(result){ // ... more stuff happening, not relevant }).then(function(result){ return result; //needed, otherwise return object, not result }); }
Comments
Post a Comment