3

So I wanted write something similar to this snipped from IndexedDB docs:

var req;
var store = getStore();
req = store.count();
req.onsuccess = function(evt) {
  console.log("success: " + evt.result);
};
req.onerror = function(evt) {
  console.error("add error", this.error);
};

https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB

And started to wonder why javascript allow definitions of (deferred?) callbacks after a call and why it doesn't cause race conditions? Can someone share some light please?

So, how does javascript make sure that async call doesn't get executed before callback is assigned?

Thanks!

pawciobiel
  • 145
  • 10
  • JS is single threaded, so the async part of the `store.count()` call will be executed after your current code and hence all callbacks have been set. – Sirko Sep 29 '15 at 09:49
  • What do you mean by "your current code"? Isn't parse time the same to execution time in js? – pawciobiel Sep 29 '15 at 09:50
  • I don't see any promises or deferreds in your code??? If there were any, the explanation would be different. – Bergi Sep 29 '15 at 12:11
  • Regardless whether there's a deferred or not, the "async" call *might* be executed immediately without any harm. It's the event which invokes the callbacks that is fired asynchronously. – Bergi Sep 29 '15 at 12:11

1 Answers1

4

JS is single threaded, so the actual execution order of your current code is as follows:

store.count();              // trigger some async code
req.onsuccess; req.onerror; // attach the callbacks
                            // some other code in the same function/scope may follow

                            // your code is finished; next item from event queue is executed

store.count();              // the async part gets executed
req.onsuccess();            // after the async part (or within it) 
                            // one of your callbacks gets executed.

So you can see, it does not really matter, when you attach your callbacks, as long as you do it, before the current function is finished and the JS event queue looks for the next code block to execute.


With respect to your comment: No, parse and execution time are not identical in modern JS engines. The engine does compile your code and then executes it. (There are some exceptions, where a fallback to the old interpreter-style is used, but that should not be relevant in most cases).

Sirko
  • 72,589
  • 19
  • 149
  • 183
  • What I really wanted to ask is how does javascript make sure that async call doesn't get executed before callback is assigned? – pawciobiel Sep 29 '15 at 10:42
  • I think async calls will be scheduled when called not when parsed right? – pawciobiel Sep 29 '15 at 10:47
  • 1
    @pawciobiel. No. The order that sirko details is correct. Think of it as executing your code straight through to the end, but any async stuff like getStore() wont actually be run until the end of your current function block, so you are safe to add event handlers after the initial assignment. As you say, they are scheduled when called, but not actually run until the next tick – Dave Pile Sep 29 '15 at 10:57