I came across this snippet from the IndexedDB documentation and wanted to write something similar:
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
This made me curious about why JavaScript allows for the definition of callbacks after a call and how it prevents race conditions. Can someone shed some light on this?
How does JavaScript ensure that an async call is not executed before the callback is assigned?
Thank you!