javascript function 1 expect return from function 2 (sync await promise.all)

2

Hello! I have a very big question regarding the use of sync / await and tbm of Promise.all.

I have the following code:

class Conexao {
  constructor(loading) {
  this.loading = loading;
}

acessar(rota) {
  return this.requisicao(rota, 1);
}


async requisicao(rota, id) {
  let rotas = ['https://willianjusten.com.br/search.json']
  rotas.push(rota);

  await Promise.all(rotas.map(function(url) {
    fetch(url).then(function(resp) {
      return resp.json();
    }).then(function(r) {
      return r;
    })
  }));
 }
}

let conn = new Conexao(true);
let result_final = 
conn.acessar('https://jsonplaceholder.typicode.com/posts/');
console.log(result_final);

As I would do when calling the access function, wait for the return of the request function and after the return is complete, the variable result_final show what returned? I'm trying this way as I showed above, however or returns me undefined or [object promise].

There is no way to 'tell' the function to access (route) "Hey, wait for the function request to solve everything that will give you an answer and then yes you will send to result_final kkkkkkk best way I found to explain kkkkkkkkkkkkkkkk >

Could someone help me and explain to me what I'm doing wrong?

The code is in jsbin: link

    
asked by anonymous 18.05.2018 / 16:34

3 answers

2

So young, come on. The async functions by definition return a Promise , if you return only one value, type the code below, the Js surrounds the heat with a Promise.

async function f1() {
  return 'f1';
}

let v1 = f1();

console.log(v1); // Prints Promise { 'f1' }

f1().then(v => {
  console.log(v); // Prints 'f1'
});

There is not much like running away from this, this is so that async functions do not freeze the execution of the script, since they are asynchronous, while they are waiting for the resolution of a Promise.

The advantage of async functions is that internally you can freeze execution by waiting for the resolution of a Promise using the keyword await . To get access to the return of an async function you will need always to use .then() , this is to some extent a protection and guarantee of the return type of the asynchronous functions (I know "guarantee and protraction "are not very usual things in Js). If asynchronous functions encapsulated return always, this would generate a lot of headache, several codes would crash while attempting to call the then method of an integer, which would be very frustrating.

How to solve your problem?

Using the famous then :

let conn = new Conexao(true);
let result_final = conn.acessar('https://jsonplaceholder.typicode.com/posts/');
result_final.then(res => console.log(res));
    
18.05.2018 / 16:45
1

Async runs, you need to wait for execution using then. Something like this:

let conn = new Conexao(true);
conn.acessar('https://jsonplaceholder.typicode.com/posts/')
.then((result_final) => {
      console.log(result_final);
   }
);
    
18.05.2018 / 16:38
0

The only thing you have to change is the last line, because result_final is a promise and you can use then to consume the value it returns when promise has completed the asynchronous part.

result_final.then(res => console.log(res));

class Conexao {
  constructor(loading) {
    this.loading = loading;
  }

  acessar(rota) {
    return this.requisicao(rota, 1);
  }

  async requisicao(rota, id) {

    let rotas = ['https://willianjusten.com.br/search.json']
    rotas.push(rota);

    // aqui eu crio a lista de Promises
    const promises = rotas.map(url => fetch(url).then(r => r.json()));

    // aqui eu espero a resposta de todas as promises direitinho
    const response = await Promise.all(promises);

    // aí respondo
    if(response) {
      return response;
    }

  }
}

let conn = new Conexao(true);
let result_final = conn.acessar('https://jsonplaceholder.typicode.com/posts/');
result_final.then(res => console.log(res));
    
18.05.2018 / 16:38