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 882253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:23:58+00:00 2026-05-15T12:23:58+00:00

In an attempt to make my jQuery code modular, i.e. reusable, I’ve placed an

  • 0

In an attempt to make my jQuery code modular, i.e. reusable, I’ve placed an ajax() function inside of another function fetch_ajax(). I’m trying to call fetch_ajax() with parameters from other places, which should simply run the contained ajax() and return the response.

But that’s not happening – instead, fetch_ajax() apparently returns false too quickly to the caller and doesn’t wait until ajax() has completed the request and gotten a response.

Here’s the code:

The fetch_ajax() function

function fetch_ajax(url) {
  $.ajax({
    type: 'GET', 
    url: url,
    success: function(response) {
      return response;
    }
  });
}

The calling function:

function caller() {
  response = fetch_ajax('script.php');
  alert(response); //this is always 'undefined'
}

In fetch_ajax(), I’ve tried not directly returning response from within ajax(), but writing it to a variable, and returning that variable after ajax() – that didn’t work either (same result).

When I place a 2nd alert right before return response; inside of the ajax() function, I’m seeing that one fire AFTER the one inside caller(). So I’m thinking that somehow, fetch_ajax() doesn’t wait for the contained function to execute before it returns false.

What am I doing wrong?

Thank you.

  • 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-05-15T12:23:58+00:00Added an answer on May 15, 2026 at 12:23 pm

    Your train of thought is wrong – the .ajax() function is written to not wait for the contained function. success is an event listener, so it is fired dynamically whenever the response arrives. You could use synchronous requests or jQuery queues to solve this, but both have disadvantages.

    See How do I make jQuery wait for an Ajax call to finish before it returns? for the synchronous request solution. It’s much simpler than the other one, but will hang your browser if the server dies right then or a hyperactive firewall blocks the answer or an angry lolcat eats your request on the way.

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

Sidebar

Related Questions

So, I'm trying to make a simple call using jQuery .getJSON to my local
I'm using the jquery validator plugin, and I'm trying to make my first attempt
Is there any attempt to re-make something like Vimeo's couch mode http://vimeo.com/couchmode in jQuery?
I'm trying to use the jQuery Validation plugin to make a multistep form with
I've been driving myself crazy trying to make this work. Variations of my attempt
I am trying to create an auto-refresh for a table using ajax/jquery, php/html, and
I am using the AjaxForm() call from jquery.form.js to attempt to run a php
I've been trying to make a multi-page poll with jQuery Mobile that is supposed
I'm trying to combine the jQuery sortable interaction and the buttonset widget to make
I'm trying to make an AJAX GET request to this URL and process the

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.