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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:53:37+00:00 2026-05-23T15:53:37+00:00

Suppose I write some JavaScript that performs an AJAX call with myCallback as a

  • 0

Suppose I write some JavaScript that performs an AJAX call with myCallback as a callback method to execute when the AJAX succeeds.

Suppose then that some other JavaScript method called myFunction is being invoked on my page when myCallback is invoked asynchronously.

Does one operation take precedence over the other? Do they both run at the same time? What happens?

  • 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-23T15:53:38+00:00Added an answer on May 23, 2026 at 3:53 pm

    Suppose then that some other JavaScript method called myFunction is being invoked on my page when myCallback is invoked asynchronously.

    Does one operation take precedence over the other? Do they both run at the same time? What happens?

    JavaScript on browsers is single-threaded (barring your using web workers, and the syntax for that is explicit). So myFunction will run until it returns — with certain caveats (keep reading). If the ajax layer completes an operation while myFunction is running (which it very well may) and needs to invoke the callback, that call gets queued. The next time your code yields, the next call in the queue will be triggered.

    It might seem, then, that we never have to worry about race conditions. That’s mostly true, but there are subtleties. For instance, consider this code:

    var img = document.createElement('img');
    img.src = /* ...the URL of the image... */;
    img.onload = function() {
        // Handle the fact the image loaded
        foo();
    };
    doSomethingElse();
    doYetAnotherThing();
    

    Since JavaScript on browsers is single-threaded, I’m guaranteed to get the load event when the image loads, right?

    Wrong.

    The JavaScript code is single-threaded, but the rest of the environment probably isn’t. So it can happen that, having set the img.src, the browser may see that it has a cached copy of the image it can use, and so it triggers the load event on the img between the img.src = ... line and the img.onload = ... line. Since my handler isn’t attached yet, I don’t get the call, because by the time I’ve attached my handler, the event has already fired.

    But you can see the effect of queuing if we reverse those lines:

    var img = document.createElement('img');
    img.onload = function() {
        // Handle the fact the image loaded
        foo();
    };
    img.src = /* ...the URL of the image... */;
    doSomethingElse();
    doYetAnotherThing();
    

    Now I’m hooking the event before setting src. If the event fires between the img.src = ... line and the doSomethingElse line (because the browser has the image in cache), the callback to my handler gets queued. doSomethingElse and doYetAnotherThing run before my handler does. Only when control passes out of my code does the queued call to my handler finally get run. The JavaScript code is single-threaded, but the environment is not.

    You can also yield to the host environment in non-obvious ways. For instance, by calling alert or its breathren confirm, prompt, etc. These functions stick out like the sore thumbs they are in modern JavaScript because they aren’t event driven; instead, JavaScript execution is suspended while a modal window is shown. But as bobince points out in his in-depth discussion here, that doesn’t mean none of your other code will run while that modal is showing. It’s still single-threaded, but the one thread is being suspended in one place (by the modal) and used to run code elsewhere in the meantime; a very fine distinction indeed. (Bob also points to some event handling — his focus example — that seems to break this rule, but it doesn’t. His example calls focus, which in turn calls the event handlers, and then returns; no different from you calling your own functions.) The key thing the items that Bob points out have in common is that your code has called into something in the host environment that does goes away and does something (shows a modal dialog, fires blur and focus handlers, etc.).

    (alert and its breathren in particular cause all sorts of nastiness, particularly around focus and blur, and I recommend avoiding them in favor of more modern techniques (which can also look about 18x better).)

    So those are the caveats mentioned at the outset of the answer. And in particular, if myFunction calls alert, at least on Firefox the ajax completion callback will get run during the alert (it won’t on most other browsers). If you’re curious to try out what does and doesn’t happen during alerts and such, here’s a test page testing setTimeout and ajax; you could extend the tests to go further.

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

Sidebar

Related Questions

I am supposed to write a program that gets some PNG images from the
Suppose I let the user to write a condition using Javascript, the user can
Suppose I have to write some GUI code as follows: widget1.addListener(event1 => handle1(event1) widget2.addListener(event2
Suppose we have some resource(a file on disk) in which we have to write
I'm using JavaScript to write some code and found an unexpected behaviour. I'm using
I'm trying to write some predicates to solve the following task (learnprolognow.com) Suppose we
Suppose I write a block to a file descriptor without doing fsync and then
I am using Visual Studio 2010 and suppose I have to write some program.
Suppose I write a program using immutable data structures in Java. Even though it
Suppose I write a library with the following: public class Bar { /* ...

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.