Why does JQuery.getJSON() have a success and a done function?

后端 未结 2 1333
独厮守ぢ
独厮守ぢ 2020-12-15 22:33

The JQuery documentation for getJSON shows an example of:

var jqxhr = $.getJSON( \"example.json\", function() {
  console.log( \"success\" );
})
  .done(func         


        
相关标签:
2条回答
  • 2020-12-15 23:15

    Initially, jQuery asynchronous functions weren't returning promises, you had to use the callback.

    Then they added the deferred (promise) system but kept the callbacks for compatibility (and because not everybody like deferred).

    From the Deferred object documentation :

    In JavaScript it is common to invoke functions that optionally accept callbacks that are called within that function. For example, in versions prior to jQuery 1.5, asynchronous processes such as jQuery.ajax() accept callbacks to be invoked some time in the near-future upon success, error, and completion of the ajax request.

    jQuery.Deferred() introduces several enhancements to the way callbacks are managed and invoked. In particular, jQuery.Deferred() provides flexible ways to provide multiple callbacks, and these callbacks can be invoked regardless of whether the original callback dispatch has already occurred. jQuery Deferred is based on the CommonJS Promises/A design.

    0 讨论(0)
  • 2020-12-15 23:29

    They are the same thing. The done function is meant to work like Promise That way you can install handlers from the result of the ajax call. It even works if you call done after the asynchronous call finished (by storing the return value)

    0 讨论(0)
提交回复
热议问题