Is it possible to add methods to JQuery's promise object?

后端 未结 3 1175
萌比男神i
萌比男神i 2021-01-17 02:23

I want to add a catch method to JQuery\'s promise object so I don\'t have to type the following every time:

.then(null,function(dat         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-17 03:02

    You can create your custom then

    $.Deferred().prototype.customthen = function(){
      $.Deferred().prototype.then.call(this, null, ,function(data){
        // handle error
        return $.Deferred().resolve().promise();
      })
    }
    

    You have added a new function named customthen to the prototype. So, this new function is available in all instances.

    Now, you can use it as follows

    .customthen();
    

提交回复
热议问题