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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:55:25+00:00 2026-05-24T17:55:25+00:00

// synchronous Javascript var result = db.get(‘select * from table1’); console.log(‘I am syncronous’); //

  • 0
// synchronous Javascript
var result = db.get('select * from table1');
console.log('I am syncronous');

// asynchronous Javascript 
db.get('select * from table1', function(result){
    // do something with the result
});
console.log('I am asynchronous')

I know in synchronous code, console.log() executes after result is fetched from db, whereas in asynchronous code console.log() executes before the db.get() fetches the result.

Now my question is, how does the execution happen behind the scenes for asynchronous code and why is it non-blocking?

I have searched the Ecmascript 5 standard to understand how asynchronous code works but could not find the word asynchronous in the entire standard.

And from nodebeginner.org I also found out that we should not use a return statement as it blocks the event loop. But nodejs api and third party modules contain return statements everywhere. So when should a return statement be used and when shouldn’t it?

Can somebody throw some light on this?

  • 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-24T17:55:27+00:00Added an answer on May 24, 2026 at 5:55 pm

    First of all, passing a function as a parameter is telling the function that you’re calling that you would like it to call this function some time in the future. When exactly in the future it will get called depends upon the nature of what the function is doing.

    If the function is doing some networking and the function is configured to be non-blocking or asychronous, then the function will execute, the networking operation will be started and the function you called will return right away and the rest of your inline javascript code after that function will execute. If you return a value from that function, it will return right away, long before the function you passed as a parameter has been called (the networking operation has not yet completed).

    Meanwhile, the networking operation is going in the background. It’s sending the request, listening for the response, then gathering the response. When the networking request has completed and the response has been collected, THEN and only then does the original function you called call the function you passed as a parameter. This may be only a few milliseconds later or it may be as long as minutes later – depending upon how long the networking operation took to complete.

    What’s important to understand is that in your example, the db.get() function call has long since completed and the code sequentially after it has also executed. What has not completed is the internal anonymous function that you passed as a parameter to that function. That’s being held in a javascript function closure until later when the networking function finishes.

    It’s my opinion that one thing that confuses a lot of people is that the anonymous function is declared inside of your call to db.get and appears to be part of that and appears that when db.get() is done, this would be done too, but that is not the case. Perhaps that would look less like that if it was represented this way:

    function getCompletionfunction(result) {
        // do something with the result of db.get
    }
    
    // asynchronous Javascript 
    db.get('select * from table1', getCompletionFunction);
    

    Then, maybe it would be more obvious that the db.get will return immediately and the getCompletionFunction will get called some time in the future. I’m not suggesting you write it this way, but just showing this form as a means of illustrating what is really happening.

    Here’s a sequence worth understanding:

    console.log("a");
    db.get('select * from table1', function(result){
        console.log("b");
    });
    console.log("c");
    

    What you would see in the debugger console is this:

    a
    c
    b
    

    “a” happens first. Then, db.get() starts its operation and then immediately returns. Thus, “c” happens next. Then, when the db.get() operation actually completes some time in the future, “b” happens.


    For some reading on how async handling works in a browser, see How does JavaScript handle AJAX responses in the background?

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

Sidebar

Related Questions

I'm doing some straight up asynchronous calls from javascript using the XMLHTTPRequest object. On
I need to write a function in JavaScript, which returns a state from calling
I have a javascript function that enters a loop and issues an asynchronous ajax
If you were an asynchronous javascript call in an Asp.Net MVC environment, and you
I've got a custom javascript autocomplete script that hits the server with multiple asynchronous
I'm looking to implement a synchronous and asynchronous version of a method within a
Let's say you have a Javascript function that calls a web service method. So
I have this code that loads an xml file using javascript: function getXmlDocument(sFile) {
I have the following JavaScript (and jQuery) code: function checkEmail(email) { if (email.length) {
I'm having a JavaScript issue where I need a function to run and complete

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.