Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:12:12+00:00 2026-06-16T00:12:12+00:00

I know that Javascript is asynchronous, but in this case I can’t see why

  • 0

I know that Javascript is asynchronous, but in this case I can’t see why what’s happens happen.

On line 27 below I call the function ‘GetProducer’ which is supposed to return data for a specific producer that I aim to use later on. However, when consoling it out it gets undefined, which is because the code on line 28 is executed before the data is retrieved (line 27).

How come this happens and what can I do to solve it?

1.     function GetProducer(id) {
2.  
3.          $.ajaxSetup ({
4.            url: "http://localhost/api/v1/producers/" + id,
5.            type: "GET",
6.            dataType: "json",
7.            cache: false,
8.            contentType: "application/json"
9.          })
10.         $.ajax({
11.             success: function(data){
12.                 return data;
13.             },
14.             error: function(xmlHttpRequest, textStatus, errorThrown) {
15.                 console.log(xmlHttpRequest);
16.                 console.log(textStatus);
17.                 console.log(errorThrown);
18.            }
19.         }).done(function(data){
20.             console.log(data);
21.         })
22.      }
23.
24.     $('.modal-btn').click(function(){
25.         var producerId = $(this).attr('id');
26.         $('#myModalLabel').html(producerId);
27.         var info = GetProducer(producerId);
28.         console.log(info); // <--- undefined
29.     });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:12:13+00:00Added an answer on June 16, 2026 at 12:12 am

    A return statement inside an ajax callback function is useless. The function is invoked asynchronously and you can’t expect anything to pass along your returned values to anything.

    If you put your console.log() call inside the “success” callback, you’ll see a value.

    You’ll probably need to change your “GetProducer” function such that it too takes a callback argument:

        GetProducer(producerId, function(info) { ... });
    

    The function would then look like this, more or less:

      function GetProducer(producerId, callback) {
        $.ajaxSetup({
          // ... same
        });
        $.ajax({
          success: function(data) {
            callback(data);
          },
          // etc
        });
      }
    

    The callback function, therefore, would be where you place the code that deals with the “producer” information. Asynchronous programming is all about giving the API functions that handle the results when the results become available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I can run an external Javascript file from within HTML with
I understand that Javascript doesn't have multiple threads, but I'd like to know if
I know that ajax calls are asynchronous but there are situations like some synchronous
I know that javascript, for example supports functions inside of functions, like so: function
I know that javascript doesn't have pointers in terms of a variable referring to
I know that in Javascript document.location.href = #my_id tells the browser to display the
I know that google's v8 compiles javascript into native machine (binary if I understand
I know that Visual Studio 2008 support JavaScript intellisense as I am using it
I know that you cannot delete a cookie set by another server in javascript.
Do you know if it is possible to make sure that a JavaScript script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.