Javascript 异步性的背后是什么 - 事件驱动系统
-
09-09-2020 - |
题
请帮助我理解这一点。
您有一个调用一些方法的函数:
function() {
methodA(function(){...});
methodB();
methodC();
}
对于没有回调或匿名函数的语言,我已经习惯了这样一个事实:在方法返回之前,执行不会继续。
因此,如果我使用回调调用 methodA,则执行必须等到该方法返回,这不是异步的,对吗?
例如,我可以将回调存储到某个对象并让 methodA 返回。然后执行方法B和方法C。当用户单击按钮时,某些处理程序会执行回调?
我得出的结论是,与 java 或 python 相比,javascript 没有什么异步性(不涉及多线程)......因为在java中回调不会是闭包/匿名方法,而是一个具有“execute”方法的对象,它会完全相同,只是稍微复杂一点......当然还有专门针对 DOM 的 JS 事件系统
解决方案
JavaScript 中的回调不会隐式添加异步行为。当调用回调函数时,它会运行 就在那时, ,就像一个普通函数一样。(其实就是一个回调函数 是 只是一个正常的功能...)
正因为如此,它 不可能的 确定示例中回调的执行何时将与其他方法相关地运行(除了它不能在之前运行) methodA
被调用)——它可以被调用 methodA
或者 methodB
或者稍后点击,或者根本不点击。(但是,除非有异常——或者其中一个函数调用了其他函数之一——那么 methodA
将在之前运行 methodB
它将依次运行在 methodC
;如果 methodA
抛出异常然后都没有 methodB
也不 methodC
将被调用)。
什么 做 添加异步行为是 异步事件源, ,例如计时器事件或 UI 操作(例如按钮单击)。
然而,请务必记住,Javascript 没有也不支持线程。在触发新的异步事件之前,Javascript 必须“停止”(执行必须从异步事件源调用的回调函数返回)。(异步事件[酌情]排队,因此如果另一个回调执行时间太长,计时器事件不会“丢失”。)
这就是为什么 while (true) {}
将使浏览器页面冻结并阻止处理按钮事件处理程序。
快乐编码。
示例案例(jsfiddle 演示):
function invokeNow(callback) {
// nothing asynchronous going on here.
// the callback is invoked right now and the result is returned.
return callback()
}
alert(invokeNow(function () { return "Hello world!" }))
function doLater(callback) {
// setup an asynchronous event
setTimeout(callback, 1000)
return "It isn't 'later' yet!"
}
alert(doLater(function () {
alert("Later!")
// note that this is running in the callback from the previous
// timer event. if this code was below the outer alert then
// it wouldn't have allowed the first timer callback to have occurred
// until the blocking while was complete
alert(doLater(function () { alert("I still ran!") }))
var end = (+new Date) + 4000
while ((+new Date) < end) { /* wait */ }
alert("I am done waiting")
}))
警告:Firefox 4 (4.0.1) 和上面的代码似乎有问题。虽然其工作原理如演示的那样,但如果超时低于约 800 毫秒,则预期顺序与实际顺序不同。我已经发帖了 所以:Firefox 4 中同步运行的异步计时器事件(“buggy”)? 所以希望会有一些解决方案。该行为在 Firefox 3、IE 9 和 Chrome 11 中按预期工作。
其他提示
function main() {
methodA(function callback(){...});
methodB();
methodC();
}
Assuming that callback is not immediately executed.
Execution order:
- methodA
- methodB
- methodC
- ... other things until the stack is empty
- callback
Javascript is sequential except if you use setInterval
, setTimeout
, or make a request to a server with a callback or using onload
. Not sure there are other cases.
If you have something like:
function methodA(fn){
...
fn();
}
Then the callback will be called when you call methodA(function(){...})