Function that returns a URL value

Asked

Viewed 54 times

0

How would I transform this function so that instead of sending the date value to prepareListDados() when called, it returns this date value when called in prepareListDados()

const getDados = async () => {
  let url = 'http://' + host + '/dados';
  xhttp.onreadystatechange = () => {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
          var data = xhttp.responseText;
          prepareListDados(data);
      }
  }
  await xhttp.open("GET", url, true);
  await xhttp.send();
}

as an example:

const prepareListDados = () =>{
let dados = getDados();
}

so the date value would be in my variable data in the prepareListDados function();

edit1:

const getDadosAsync = () => new Promise((resolve, reject) => {
  let url = 'http://' + host + '/dados';
  xhttp.onreadystatechange = () => {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      var data = xhttp.responseText;
      resolve(data);
    }
  }
  xhttp.open("GET", url, true);
  xhttp.send();
});

const prepareListDados = async () => new Promise((resolve, reject) => {
    let dados = await getDadosAsync();
    resolve(dados)
});

const setList = async () => {
 let dados = await prepareListDados();
}

something like that

  • You want to turn an asynchronous operation into synchronous, it does not give.

  • and what solution would you give me to receive data from a url in a synchronous function?

  • 1

    In the browser? The recommendation is never to search for data synchronously, as this locks the page until the answer is received.

1 answer

1

Return a Promise, and call the resolve of the granted value, which will be obtained when you use await.

const getDadosAsync = () => new Promise((resolve, reject) => {
  let url = 'http://' + host + '/dados';
  xhttp.onreadystatechange = () => {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      var data = xhttp.responseText;
      resolve(data);
    }
  }
  xhttp.open("GET", url, true);
  xhttp.send();
})

const prepareListDadosAsync = async () => {
  let dados = await getDadosAsync();
}

An even easier way to do this is by using fetch, that already returns a default file:

const getDadosAsync = async () => {
  let response = await fetch('http://' + host + '/dados');
  return await response.text();
}

const prepareListDadosAsync = async () => {
  let dados = await getDadosAsync();
}
  • 1

    Good, I was going to suggest that you give an example with fetch. Since it is to use async/await, it certainly makes more sense (including because they both have the same compatibility problems).

  • It worked exactly as planned, thanks! but a curiosity now, has how to make a chain of these Promise? type in example: if the prepareListDadosAsync = () => new Promise((resolve, Reject) => ... would it work? and so on

  • @Vitorpereira, I did not understand very well the doubt. It would be better to edit the question or create a new one with all the details of what you want.

  • @user140828 Look at edit1, something like that

Browser other questions tagged

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