Say you have the following code snippet:
function someProcess() { var deferred = $.Deferred(); apiCall(function (recvData) { deferred.resolveWith(null, [recvData]); }); return deferred.promise(); } function mainFunction() { $.when(someProcess()) .then(someOtherProcess); }
In this example, I only need to wait until one pending request is resolved. In this case, what is the difference (if any) between writing the second function, as described above, and writing it as follows:
function mainFunction() { someProcess() .then(someOtherProcess); }
I mean, I like to write it the first way, because it makes it clear that we are using jQuery pending objects, but I'm curious if this is necessary in this case.
edit: I set a typo in the then () call. Thanks for catching this.
edit: Thanks for the answer nrabinowitz. I think you attached points that I was not sure about when () compared to using a jQuery deferred instance. I went and corrected my code again to return a promise instead of the whole pending object. This is how I do it in my actual code right now, just forgot to add it here.
source share