Are event handlers guaranteed to complete before AJAX callbacks are invoked?

后端 未结 4 713
面向向阳花
面向向阳花 2020-12-31 11:21

Suppose I have a event handler which makes two AJAX calls to the server:

$(\"#foo\").click(function(){ 
    $.get(\"bar\", function(){ alert(\"Hello\"); });
         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 12:22

    An interesting twist to this would be instead of $.get(), lets say we were using promises that we get from somewhere else (i.e. the actual async call was made elsewhere) (-Why would we have such a situation? Well maybe we have a memoized query function.)

    Now if one were using jQuery promises, the callbacks would be invoked synchronously if already resolved. Is that ever an issue? Well depends on your requirement. If it does then you could wrap the callback code in a setTimeout(cb, 0).

    On the other hand, what if you wanted the callbacks to be preempted? See my examples here

提交回复
热议问题