jQuery: What is the difference between deferred.always() and deferred.then()?

后端 未结 4 1857
离开以前
离开以前 2020-12-29 04:03

Seems to me that both does the same thing.

Docs:

  • deferred.always()

  • deferred.then()

4条回答
  •  借酒劲吻你
    2020-12-29 04:36

    The big benefit of then (as of 1.8) is the capability to chain tasks explicitly because it returns a promise which will be resolved with the result of the callback(s)

    Example from documentation:

    var request = $.ajax( url, { dataType: "json" } ),
        chained = request.then(function( data ) {
          return $.ajax( url2, { data: { user: data.userId } } );
        });
    
    chained.done(function( data ) {
      // data retrieved from url2 as provided by the first request
    });
    

提交回复
热议问题