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.
– bfavaretto
and what solution would you give me to receive data from a url in a synchronous function?
– Vitor Pereira
In the browser? The recommendation is never to search for data synchronously, as this locks the page until the answer is received.
– bfavaretto