0

I encapsulated the JQuery Get function as such in a javascript file:

      var Ajax = {
        get: function (url) {
             $.get(
                {
                    url: url,
                    timeout: 120000,
                    cache: false,
                    async: true,
                    dataType: "json",
                    contentType: "application/json; charset=utf-8"
                }
            ).done(function() { DoSomethingDoneFirst(); }).fail(function (x, y, z) { DosomethingFailedFirst());
        }
    }

As you can see i am utilizing the fail and done method of the promise object within this encapsulated method because I need to do some actions when the request is finished. How do I also return the promise object so that any calling function utilizing this Get method can append their own done, fail, etc methods.

Looking at the example below, a developer can utilize the Get function in their own code. I would expect 1) the Ajax Get request to be invoked 2) when done - the DoSomethingDoneFirst would be called and 3) the developers DoSomethingSecond function would be called. I want to return the promise object from the Get method, so that the developer can "append" thier own done, fail, always, etc methods. How can I accomplish this?

    function Test() {
        if (1==1){
       Ajax.get(http://www.example.com).done(DoSomethingSecond)
    }

}
DanB
  • 11
  • 2

0 Answers0