3
I am very doubtful about the use of async/await
and also 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 function access, wait for the return of the request function and after the return is completed, the variable result_final
show what returned?
I’m trying this way as shown above, but either return to me undefined
or [object promise]
.
There is no way to "say" to access function(route):
- "Hey, wait for the function requisicao solve everything she will give you an answer and then yes you send to
result_final
?
The best way I could explain...
Could someone help me and explain to me what I’m doing wrong?
The code is in jsbin:
Ahh.. Now yes I understood right.
– Rafael Pelizza
For example, for example... I already solve it so it returns to function access with the then and only after the resolution return to variable result_final unfortunately there is no way right? Man, seriously.. I broke my head with this since yesterday.. Very orbigado same to all!
– Rafael Pelizza
Like, you can’t guarantee that the result will be available outside of the
then
... You’ll only be able to use it inside– Gabriel Carneiro