1

I'm trying to execute three similar ajax calls in order whilst returning a Boolean for each.

Example Below

function Request(){

var _req1_successful = false;
var _req2_successful = false;
var _req3_successful = false;

_req1_successful = RequestCall(**params**)
_req2_successful = RequestCall(**params**)
_req3_successful = RequestCall(**params**)

**Carry out results etc...**
}

function RequestCall(**Params**) {
 $.ajax({"URL",
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: "{**Params**}",
        success: function(data) {
           return true 
        },
        error: function(x, e) {
            return false;
        }
    });
}

I've looked at callback's but either i'm missing something or you cant return values using them. (Forgive if incorrect im fairly new to JS)

I read adding a Aysnc="False" property to the ajax call would work but didn't seem to...

So in a nutshell im trying to execute each call sequentially then once all has been carried out execute the logic afterwards.

Any ideas or pointers?

L Riley
  • 337
  • 4
  • 15
  • Your `RequestCall()` function isn't returning anything as the AJAX call is asynchronous (it's what the 'A' stands for). You need to use the clalback pattern properly. See the duplicate for more information. – Rory McCrossan Feb 13 '18 at 14:44
  • [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) or [async/await](https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9) – Jeremy Thille Feb 13 '18 at 14:44
  • The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, you can use async await – Naramsim Feb 13 '18 at 14:48

0 Answers0