I'm trying to resolve promises from a nested for loop:
let fillInTable = () => {
let result = []
$.getJSON(`https://kvdb.io/${bucket}/?format=json`).then((data) => {
data.map(each => $.getJSON(`https://kvdb.io/${bucket}/${each}`).then(o => {
result.push(o)
}));
});
return result
}
let random = fillInTable(); // appears to be []
// TODO: Wait for random to receive all the information from fillInTable() before executing anything else
The random
is always empty, which I guess by default getJSON
is async. I'm trying to use await
but it doesn't work since it's not in an async function.
I tried to push all promises in an array and then use Promise.all([...])
but doesn't work either.
Does anyone have a solution?