Reputation: 827
I've written a function that I want to return the results of a successful deferred function. However, since the value I want returned is in the scope of the doneCallbacks
, the runSearch()
function returns undefined when running console.log(runSearch())
. How can I have the containing function return the returned value of a successful deferred.then()
?
function runSearch(){
$.get( "search.php" ).then(
function() {
// I want runSearch() to return the results of this function
}, function() {
console.log( "$.get failed!" );
}
);
}
EDIT: Thanks for all the help everyone. Since my goal was to user the return value in a function, I just included the function in the doneCallbacks
.
function runSearch(){
$.get( "search.php" ).then(
function() {
var returnValue = 'Return Value'
function doSomethingWithReturn(returnValue) {
console.log(returnValue);
}
doSomethingWithReturn(returnValue);
}, function() {
console.log( "$.get failed!" );
}
);
}
runSearch(); // successfully logs returnValue to console.
May not be the most elegant solution, but works for this situation.
Upvotes: 0
Views: 1070