assync does not work in ajax

Asked

Viewed 33 times

-3

I’m trying to use async to return the amount the bank will bring, but it’s not working

async function emailExi(email){ 
    const teste = await $.ajax({
        url: "../banco/emailexi.php",
        method: "POST",
        data: {email: email},
        success: function(data){
            console.log(data)
            return data
        }
    })
}

I am testing with a console.log(), and returns promisse{pending}

Here’s the version with the then, I don’t know if it’s right

async function emailExi(email){ 
    const teste = await $.ajax({
        url: "../banco/emailexi.php",
        method: "POST",
        data: {email: email},
    }).then(function(data){
        return data
    })
}

var tr = emailExi("aa")

console.log(tr)

So I have 2 functions:

function valiEmail(email){
let erro = false

if (emailExi(inputEmail)){
erro = "email já existe. Por favor coloque outro email"
}

return erro
}
async function emailExi(email){ 
    const teste = await $.ajax({
        url: "../banco/emailexi.php",
        method: "POST",
        data: {email: email},
    })
    return teste
}

i want to return if it is true or false there for valiEmail function, how do I do this?

  • Have you tried using the then to capture the information?

  • I tried, but I don’t know if I did it right

1 answer

1

If using the await, doesn’t need the success or then within the function. You will need then only when it is not already in asynchronous scope.

async function get_title(id) {
  const data = await $.ajax(`https://jsonplaceholder.typicode.com/todos/${id}`);
  
  return data.title;
}

// Pode obter o resultado utilizando o then
get_title(1).then(title => console.log(title));

// Ou usando o await dentro de uma função async
(async () => {
  const title = await get_title(1);
  console.log(title);
})()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  • i put the then out of the function, but how do I return the value? I need to return the query value

  • It’s in the answer, the function get_title returns the query value. The question is that since it is an asynchronous function, you need to access the return using the await, when inside an asynchronous block, or capturing promise resolution via function then. The answer shows the two examples.

  • actually returns, but I want to know how to return this value to 1 function that I called

  • I have a function that calls this function ajax, I need to resume the value for this 1 function, not for a 3 function

  • Again, there are two examples that do this in the answer. What you didn’t understand?

  • So the answer comes in these example functions, only I wanted to put this value in a variable and go back to the mother function, where all this started. I’ll edit the question so you understand better

  • @someone const existe = await emailExi(inputEmail), just do this before your if and make if (existe)

  • this time had another problem, gave error pq so to use the await in asynchronous functions, then transformed the function into asynchronous, but when hr return the value of this function, returns a promise. And I even tried to separate in another function, but then the code does not wait for that other function to end

  • Clearly you still don’t quite understand the concepts of asynchronous functions, so I suggest you review your studies before proceeding. A documentation that usually helps me understand some concepts is MDN, see the page on asynchronous functions: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await

  • I really don’t understand, I know what is asynchronous and synchronous, but in practice I can’t use, I’ve been trying to use, and even put nothing, the worst is that I don’t understand any of these Docs. vlw for the help

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.