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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:37:45+00:00 2026-06-01T05:37:45+00:00

Suppose I have a event handler which makes two AJAX calls to the server:

  • 0

Suppose I have a event handler which makes two AJAX calls to the server:

$("#foo").click(function(){ 
    $.get("bar", function(){ alert("Hello"); });
    $.get("baz", function(){ alert("World"); });
});

I realize that the order in which the callbacks are invoked is nondeterministic, since it depends on how long each request takes, etc.

But here’s my question: is it guaranteed that I’ll reach the end of my event handler before either of the callback functions are invoked? I’ve read that all Javascript for a page executes in a single thread, so I think that implies that my click event handler is guaranteed to complete before any of the callbacks can be invoked.

Is this correct? Or is it possible that the first request might be complete AND the first callback executed before we even get to the end of our event handler?

  • 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-01T05:37:46+00:00Added an answer on June 1, 2026 at 5:37 am

    Yes, this is guaranteed and you are right – there is just a single thread (ignoring web-workers for the moment). When a piece of JavaScript code executes (occupies the execution thread) and an AJAX callback arrives (or any other GUI event, timeout, etc.) it is queued and waits until the execution thread is free (current piece of code finishes).

    JavaScript engine will never interrupt running code to handle incoming event – events will always gently wait in a queue. This is the reason why the GUI appears to be freezing when CPU-intensive code is executing – no events are handled. Also this is why synchronous AJAX requests are bad.

    See also

    • JavaScript equivalent of SwingUtilities.invokeLater()
    • "atomic" operation desturbed by asynchronous ajax callbacks
    • Are there any atomic javascript operations to deal with Ajax's asynchronous nature?
    • how is async programming (promises) implemented in javascript? isn't javascript a ui-threaded environment?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this jQuery code. $(document).delegate('a[title]', 'click', function(event) { var txt = //
I have a custom event handler, or I suppose some call it a custom
Suppose I have a non-recurring event that needs to be raised X seconds from
Suppose I have an events controller. If I request /events by GET it will
I have a remote server which handles various different commands, one of which is
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
I have a class which makes a url request and stores data from that
Suppose I have a Monkey class which sometimes needs to acquire an instance of
Suppose I have one div in my page. how to detect the user click
Suppose I have an event KeyPress subscribed to by various classes. Assume that Class

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.