2
I recently asked that question:
Wait for the variable to be completed
The moderator Sergio helped me with the issue, but I’m still having problems with compatibility with old browsers. There is a way to accomplish this without using Promises and that runs on older browsers?
Follow the example code:
function getNome(nome) {
const url = 'https://httpbin.org/get?nome=' + nome;
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) resolve(JSON.parse(xhr.responseText));
else reject(xhr.status);
};
xhr.send();
});
}
const nomes = ["lucas", "pedro", "joao"];
Promise.all(nomes.map(getNome)).then(res => {
console.log(res);
});
What I want is to go through an array, and make a request in each name and saving the answer in an object, while the code continues running and doing other tasks, when all the answers are in an object the code triggers another function.
If the problem is Promises support, why not use a polyfill? Or better yet, why don’t you use a modern Javascript transpilator for old Javascript, so you don’t have to keep making ugly code-- the transpilator does it for you and you don’t even need to see.
– Pablo Almeida
I still can’t use the Abel
– Stan