2
Good morning!
After breaking my head and not finding the solution, I came to ask Sopt’s friends. It is possible to create an asynchronous foreach?
'Cause the next thing, look at my code:
async function generateWishlist() {
var products = [];
var productLength = 0;
JSON.parse(getCookieVal('whishlistLEL')).forEach(function(e) {
fetch("/api/catalog_system/pub/products/search?fq=productId:" + e)
.then(a => a.json())
.then(a => {
products.push(a[0]);
console.log("Estou passando por aqui hahah, estou te trolando, Lucas")
productLength++;
});
});
return new Promise(function(resolve) {
resolve([products, productLength]);
});
}
async function initWishlist() {
if(!verifyWishlist()) {
console.log("Você não possui produtos na lista de desejos!");
return false;
}
await generateWishlist()
.then(function(resolve) {
console.log(resolve);
})
}
initWishlist();
Do not consider the functions verifyWishlist()
and getCookieVal()
they are being used but are not part of the problem. (just to leave the example of the smaller code)
What I got back is this right here in the picture:
The array came ok, the Indice 0 brought normally, the Indice 1, did not count what I put there, I believe because the foreach is not asynchronous...
Could I create this foreach asynchronously?