Function Returns [Object Promise]

Asked

Viewed 1,488 times

3

function that takes the data from a url:

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();
});

The date value and something like: test,123change,IMPEXPROS,Armento:172.20.1.1,172.20.1.2,172.20.1.3,172.20.1.4,172.20.1.5,172.20.1.6,255.255.255.255.0,192.168.1,192.168.1.140,255.255.255.255.0,192.168.1.121:

and take the data in this function:

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

  let arrParams = dados.split(':');
  let nomes = arrParams[0].split(',');
  let ips = arrParams[1].split(',');
  let arrDados = [];

  arrDados.push({
   ssidAP: nomes[0],
   senhaAP: nomes[1],
   ssidSTA: nomes[2],
   senhaSTA: nomes[3],
   ipLocal: ips[0],
   ipmodulo1: ips[1],
   ipmodulo2: ips[2],
   ipmodulo3: ips[3],
   ipmodulo4: ips[4],
   ipmodulo5: ips[5],
   mascaraAP: ips[6],
   gatewaySTA: ips[7],
   localSTA: ips[8],
   mascaraSTA: ips[9],
   servidorSTA: ips[10],
 });

  let a = setSSIDAP();
  let b = setSSIDSTA();

  if (a == "null" && b == "null"){
    return "null";
  }
  if(b == "null" && a != "null"){
    return a + ","+ arrDados.ssidSTA + "," + arrDados.senhaSTA;
  }
  if(a == "null" && b != "null"){
    return arrDados.ssidAP + "," + arrDados.senhaAP + "," + b;
  }
  if (a != "null" && b != "null"){
    return a + b;
  }
}

edit1: I receive the data of this function as follows:

const sendConf = async () => {
    let users = setUsers();

    if (users){
        //let url = decodeURI("http://" + host + "/config?a=" + localreles + "&b=" + apwifi + "&c=" + ips +"&dd=null:&d=" + users.usuarios + "&e=" + users.senhas + "&f=" + users.tags + "&g=" + users.horarios);
        let url = decodeURI("http://"+host+"/config?a=null:&b="+texto1()+":&c="+texto2()+":&d=null:&e=" + users.usuarios + "&f=" + users.senhas + "&g=" + users.tags + "&h=" + users.horarios);
        xhttp.onreadystatechange = () => {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                var data = xhttp.responseText;
                console.log(url);
                alert(url);

            }
        }
        await xhttp.open("GET", url, true);
        await xhttp.send();
    }
}

and the return of this function text1() is returning [Object Promise], why not the data? I tested call text1(). then() and returned the same thing.

1 answer

3


When you have the floor async in the declaration of a function it will return a Promise whose value you can use when Promise is solved.

To use the value of const texto1 = async ()=>{ you have to use the .then(resultado => { and within that then then you can use the value that resultado may contain.

const fnAtrasada = async() => new Promise(res => {
  setTimeout(() => res('Olá'), 2000);
});
const fn = async(nome) => {
  console.log('Função iniciada...');
  const saudacao = await fnAtrasada();
  return `${saudacao} ${nome}!`;
}

fn('Ana').then(string => console.log(string));

To use many like that you must use the Promise.all, where you pass an array of files, and within the .then() from it you receive an array with the result of each file in it. An example would look like this:

Promise.all([texto1(), texto2()]).then(resultados => {
  let url = decodeURI("http://" + host + "/config?a=null:&b=" + resultados[0] + ":&c=" + resultados[1] + ":&d=null:&e=" + users.usuarios + "&f=" + users.senhas + "&g=" + users.tags + "&h=" + users.horarios);
  // e o resto do ajax aqui...
});

Browser other questions tagged

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