Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been doing a lot of reading online trying to figure out how to write asynchronous JavaScript code. One of the techniques that has come up a lot in my research is to use callbacks. While I understand the process of how to write and execute a callback function, I'm confused why callbacks seem to automagically make the JavaScript execution asynchronous. So, my question is: how does adding in callback functions to my JavaScript code make said code automagically async?

share|improve this question
You might be interested in reading how the browser achieves this within a single thread, written by John Resig: – Jalayn Apr 10 '13 at 17:05
Thank you, @Jalayn, I'll check it out! – Zach Dziura Apr 10 '13 at 18:28
@Jalayn. Thanks. Your comment made all the difference after reading the several answers. – MegaRacer Dec 15 '15 at 3:13
up vote 20 down vote accepted

It doesn't. Just taking a callback or passing a callback doesn't mean it's asynchronous.

For example, the .forEach function takes a callback but is synchronous.

var available = false;
[1,2,3].forEach( function(){
    available = true;
//code here runs after the whole .forEach has run,
//so available === true here

The setTimeout takes a callback too and is asynchronous.

function myFunction( fn ) {
    setTimeout( function() {
    }, 0 );

var available = false;
myFunction( function() {
    available = true;
//available is never true here

Hooking to any asynchronous event in Javascript always requires a callback but that doesn't mean calling functions or passing them around is always asynchronous.

share|improve this answer
@SteveEvers I guess you could edit the MDN article then. Callback is a very common term in javascript for any function passed to another that calls you back using the function you gave... why complicate it? Edit: Callback is also used in specs – Esailija Apr 10 '13 at 18:52
@SteveEvers "callback implies asyncrony" is a C#/Microsoft idiosyncrasy. – Esailija Apr 10 '13 at 18:58
@MikeBrown I guess I read too much into his profile and this - the most upvoted answer certainly gives an idea that "callbacks imply asynchrony". – Esailija Apr 10 '13 at 19:29
But that being said @SteveEvers is also wrong. Callbacks don't imply asynchrony, just that someone else will be responsible for "calling back". – Michael Brown Apr 10 '13 at 19:34
+1 because Steve Evers is wrong – slebetman Apr 11 '13 at 1:45

The secret of the "magic" is that the events that you are assigning the callbacks to are asynchronous. They're implemented "under the hood" to take care of whatever they're doing (such as retrieving something from a remote server) in the background, outside of the JS sandbox. And then once they're done with their work, they give the JS engine a message to call an event. When the JS engine is finished with whatever it's currently doing, it will call any events that are queued up (or wait for a new message) and then your callback is "magically" invoked asynchronously!

(NOTE: This is a very high-level, conceptual overview of the topic that doesn't go into details, because different JS engines are going to implement things in different ways. But this is the general idea of how it works.)

share|improve this answer
+1 for high-level, conceptual overview – pmverma May 29 '15 at 6:50
thanks helped alot. – Stav Alfi Sep 23 '15 at 11:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.