0

I'm struggling now two days trying to convert a simple AJAX function with async:false into a nice jQuery Deferred() one. Everything I tried to return the AJAX response wont work :-(

this is my old (short) version of code:

function uiText($textID) {
   var text = $.ajax({
     url: "uitext.php",
     async: false,
     success: function(response){}
   };
   return text.response;
}
console.log(uiText('foo'));  //shows me the response from uitext.php

How I can do this with $.Deferred and/or $.when().done()

Thx Mike

mikexmagic
  • 63
  • 6

1 Answers1

0

$.Deferred and/or $.when().done()

Don't. That's obsolete. $.ajax returns a promise compatible object.

function uiText($textID) {
   const request = $.ajax({
     url: "uitext.php"
   });
   return request;
}

uiText('foo').then( response => console.log(response) );
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I read so much about using promise stuff and I use it already in other situations. But i this case, I need that uiText returns the ajax response... not the promise object. Is this possible? – mikexmagic Nov 19 '18 at 13:32
  • @mikexmagic — No. `$.Deferred` and `$.when().done()` are just ways to create promises. – Quentin Nov 19 '18 at 13:33