所以我想写一些类似于从 IndexedDB 文档中截取的内容:
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
然后开始想知道为什么 javascript 允许在调用后定义(延迟?)回调,为什么它不会导致竞争条件?
有人可以分享一些光吗?
那么,javascript 如何确保在分配回调之前不会执行异步调用?
谢谢!
JS是单线程的,所以你当前代码的实际执行顺序是这样的:
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.
所以你可以看到,当你附加你的回调时,这并不重要,只要你在当前函数完成之前执行它并且 JS 事件队列寻找下一个要执行的代码块。
关于您的评论:不,解析和执行时间在现代 JS 引擎中并不相同。引擎会编译您的代码,然后执行它。 (有一些异常(exception),使用旧解释器样式的回退,但在大多数情况下这不相关)。