Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How do I return the response from an asynchronous call?

How do I return the response/result from a function foo that makes an asynchronous request?

I am trying to return the value from the callback, as well as assigning the result to a local variable inside the function and returning that one, but none of those ways actually return the response — they all return undefined or whatever the initial value of the variable result is.

Example of an asynchronous function that accepts a callback (using jQuery's ajax function):

function foo() {
 var result;
 $.ajax({
 url: '...',
 success: function(response) {
 result = response;
 // return response; // <- I tried that one as well
 }
 });
 return result; // It always returns `undefined`
}

Example using Node.js:

function foo() {
 var result;
 fs.readFile("path/to/file", function(err, data) {
 result = data;
 // return data; // <- I tried that one as well
 });
 return result; // It always returns `undefined`
}

Example using the then block of a promise:

function foo() {
 var result;
 fetch(url).then(function(response) {
 result = response;
 // return response; // <- I tried that one as well
 });
 return result; // It always returns `undefined`
}

Answer*

Draft saved
Draft discarded
Cancel
4
  • 5
    Could you explain how the if (--expecting === 0) part of the code works please? The callback version of your solution is working great for me, I just don't understand how, with that statement, you are checking the number of responses completed. Appreciate it's just lack of knowledge on my part. Is there an alternative way that check could be written? Commented May 28, 2017 at 10:21
  • 5
    @Sarah: expecting starts out with the value of array.length, which is how many requests we're going to make. We know the callback won't be called until all of those requests are started. In the callback, if (--expecting === 0) does this: 1. Decrements expecting (we've received a response, so we're expecting one fewer response) and if the value after the decrement is 0 (we're not expecting any more responses), we're done! Commented May 28, 2017 at 18:31
  • 2
    @Henke - I think it is indeed personal preference, and while normally I'd prefer to log raw data and let the console handle it, in this specific case I think you're right about the change. Thanks! :-) Commented May 6, 2021 at 11:27
  • 2
    Out of convenience for myself (and others?), adding a link to a related answer: How to make many asynchronous calls and wait for them all. Commented Jun 1, 2021 at 14:01

lang-js

AltStyle によって変換されたページ (->オリジナル) /