1

I can see that this matter has been discussed, but I am not making a connection with the answer given and my problem.

I am using jQuery 1.4.2 and I wish to chain two ajax calls together, passing the response for the first as an input to the second.

I have this:

 ftpReport(taskID, debugMode)
 .then((errorFlag) => {
     return updateTask(taskID, errorFlag, debugMode);  
 })
 .done(() => {});

The function ftpReport begins:

 function ftpReport(taskID, debugMode) {
        return $.ajax({

hence, I am returning this function so that it can be then-ned with the following call to updateTask

I have attempted to return a value from ftpReport (during both success and error fns) in the expectation that the returned value would be considered as errorFlag to the next step, of calling updateTask

 success: function () {
     return 0;
 },
 error: function (XMLHttpRequest, textStatus, errorThrown) {
     return 1;
 }

The value which appeared as errorFlag was ""

How do I correct my code so that either the 1 or the 0 reported by ftpReport will be passed to updateTask?

Basil Bear
  • 433
  • 3
  • 15
  • "I am using jQuery 1.4.2" — STOP! That version of jQuery no longer gets security updates. It has *publicly known* [security problems](https://www.cvedetails.com/vulnerability-list/vendor_id-6538/product_id-11031/version_id-235481/Jquery-Jquery-1.4.2.html). Upgrade to a supported version of jQuery. – Quentin Feb 15 '19 at 11:46
  • One day, hopefully :) – Basil Bear Feb 15 '19 at 11:53
  • Try to use latest jQuery and follow this answer https://stackoverflow.com/a/36255998/3835843 – Arif Feb 15 '19 at 12:03

1 Answers1

1

The promise returned from $.ajax will either resolve to the successful response or trigger an exception (in the case of an error).

It does not adopt the values returned from the success and error callbacks. The return values from those are ignored.

To convert them to a promise that resolves as 1 or 0, you need to create that promise explicitly.

return new Promise( res => {
   $.ajax({ 
       // Other options
       success: () => res(0),
       error: () => res(1),
   });
});
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335