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

  • Home
  • SEARCH
  • 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 5846869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:38:16+00:00 2026-05-22T12:38:16+00:00

please help me understand this. You have a function that calls a few methods:

  • 0

please help me understand this.

You have a function that calls a few methods:

function() {
   methodA(function(){...});
   methodB();
   methodC();
}

From languages that don’t have callbacks or anonymous functions I got used to the fact, that the execution doesn’t go on until a method returns.

So that if I called methodA with the callback, the execution would have to wait until the method returns which wouldn’t be asynchronous, right ?

So that, for instance, I can store the callback to some object and have the methodA return. Then methodB and methodC are executed. And when user clicks a button, some handler executes the callback ?

I’ve come to conclusion that there is nothing asynchronous about javascript in comparison with java or python (not regarding multithreading)…. because in java the callback wouldn’t be closure/anonymous method, but an object with “execute” method and it would be exactly the same, just a little bit more complicated … Of course there is this JS event system specific for DOM

  • 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-22T12:38:17+00:00Added an answer on May 22, 2026 at 12:38 pm

    Callbacks in JavaScript do not implicitly add asynchronous behavior. When a callback function is invoked, it runs right then, just as a normal function. (Actually, a callback function is just a normal function…)

    Because of this, it is impossible to determine when the execution of the callback in the example will run in relationship to the other methods (except that it can’t run before methodA is invoked) — it could be called from methodA or methodB or from a click later, or not at all. (However, unless there is an exception — or one of the functions invokes one of the other functions — then methodA will run before methodB which will in turn run before methodC; if methodA threw an exception then neither methodB nor methodC would be invoked).

    What does add asynchronous behavior is an asynchronous event source, such as a timer event or UI action such as a button click.

    However, it is important to keep in mind that Javascript does not have or support threading. The Javascript must “stop” (execution must return from the callback function invoked from an asynchronous event source) before a new asynchronous event can be triggered. (Asynchronous events are queued [as appropriate] so a timer event won’t be “lost” if another callback takes too long to execute.)

    This is why while (true) {} will make a browser page freeze and prevent button event handlers from being processed.

    Happy coding.


    Example cases (jsfiddle demo):

    function invokeNow(callback) {
       // nothing asynchronous going on here.
       // the callback is invoked right now and the result is returned.
       return callback()
    }
    alert(invokeNow(function () { return "Hello world!" }))
    
    function doLater(callback) {
        // setup an asynchronous event
        setTimeout(callback, 1000)
        return "It isn't 'later' yet!"
    }
    
    alert(doLater(function () {
        alert("Later!")
        // note that this is running in the callback from the previous
        // timer event. if this code was below the outer alert then
        // it wouldn't have allowed the first timer callback to have occurred
        // until the blocking while was complete
        alert(doLater(function () { alert("I still ran!") }))
        var end = (+new Date) + 4000
        while ((+new Date) < end) { /* wait */ }
        alert("I am done waiting")
    }))
    

    Warning: There seems to be an issue with Firefox 4 (4.0.1) and the code above. While it works as demonstrated, the expected order differs from the actual order if the timeout is below about 800ms. I have posted SO: Asynchronous timer event running synchronously (“buggy”) in Firefox 4? so hopefully there will be some resolution. The behavior works as expected in Firefox 3, IE 9, and Chrome 11.

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

Sidebar

Related Questions

I cannot understand how this is possible. Please help!! I have an app with
I have a heartbeat interval which calls a function every few seconds. This function
Hi Guys could you please help me refactor this so that it is sensibly
I have this javascript function I am trying to understand. I don't know if
Can someone please help me understand how I would write this. If i am
Please help me to understand below code. This is the script for drag and
Please help! Background info I have a WPF application which accesses a SQL Server
Please help me convert this line to C#. objManagementBaseObject.SetPropertyValue(hDefKey, CType(&H & Hex(RegistryHive.LocalMachine), Long)) Related
Hi Guys can you please help me with this error? What is it? Server
Please help me understand the root cause of the following behaviour. In file a.cpp

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.