Run Ajax first

0

The last line of the code below is the first one to be executed, and I need the function containing this ajax to return true or false, but when I put it to return within success, error or complete it does not work. >

error: function (response) {
        if (((response.responseText.split("{")[1]).split("}")[0]).split(":")[1]) {
            confirmationValue = true;

        } else {
            confirmationValue = false;
        }
    },
    complete: function () {
        console.log(confirmationValue);
        return confirmationValue;
    }
});
console.log(confirmationValue);

Thank you!

    
asked by anonymous 09.10.2018 / 15:20

1 answer

0

You can use the sync and await of ES6, this way you make the rest of your code wait for the result of your ajax function to continue the code, in your case it waits for the boolean return to give sequ

    async  function executar(){
    return $.ajax({
    'url': 'https://swapi.co/api/people/1',
    'metod': 'GET',
    success: async function(data){
        return data
    }
  })
}

async function chamaFuncao(){
  await executar()
  .then((res) =>{
        alert(res.name)
  })    

  alert('Continua a execução')

}

chamaFuncao();

See the example in JSFiddle

    
09.10.2018 / 19:30