Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to JavaScript and from various resources i have read that JavaScript functions are Asynchronous if they are coupled with Callbacks. After searching rigorously for 10+ days on web i could not find an explanation on how Callbacks in JavaScript run Asynchronously. Some examples with AJAX are given but they do not provide a clear answer, can anyone explain how callbacks in JavaScript run Asynchronously for the below code?

function myFunc(a,b,callback){
    var callbackValue = callback();
    var add= a+b;
    var subt= a-b;
    var mult= a*b;
    var div= a/b;
    ...
    ...
    ...
    ...
    ...
    var totalValue= add+callbackValue;
}

function myFunc(a,b,function(){//complex scientific operation which takes 10 secs });

As i am using a callback in "myFunc" in the above code, does that mean that when callback() is invoked in "myFunc", it runs asynchronously and the program flow continues with var add= a+b; var subt= a-b; ......... ....... without waiting for result of callback();?

share|improve this question
5  
No, simply passing functions as an argument doesn't make anything asynchronous. The asynchronous things in JavaScript are things like setTimeout, setInterval, event handlers, web workers, etc. So depending on what the callback is doing when you say "which takes 10 secs", if it doesn't use an asynchronous feature, it's not asynchronous. – Ian 12 hours ago
1  
An example of an asynchronous callback: jsfiddle.net/92yxc . The reason a callback is needed here is because the anonymous function run by setTimeout is scheduled to run 1000ms from the time setTimeout was executed. Therefore, the test function can't return the value needed, and instead you have to use a callback so that the callback has access to the value when it's available (1000ms from then) – Ian 12 hours ago
@Ian: I think you should make that an answer. – Felix Kling 9 hours ago
add comment (requires an account with 50 reputation)

2 Answers

asynchronous - not occurring at the same time.

synchronous means that the page will have to wait while JavaScript executes. With asynchronous the page does not have to wait for the execution of jaJavaScript

share|improve this answer
add comment (requires an account with 50 reputation)

Yes and no. If you don't use setTimeout/setInterval, then it runs synchronously.

// alert after 5 seconds
function func(f) {
    f();
    alert('Hello');
}

func(wait5);

But if you use setTimeout, it can run in a separate execution context. I'd Google this, since it's rhetorical to explain.

// alert immediately, then wait 5 seconds
function func(f) {
    setTimeout(f, 0);
    alert('Hello');
}

func(wait5);

Of course, wait5 in this case would be a function that waits 5 seconds. You can create this function if you'd like, just run a while loop until 5 seconds has elapsed ( check using new Date().getTime() )

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

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.