Basically, I have a promise that has multiple API calls in a loop that appends the results into an array, which in turn is being resolved by Promise.all
. However, the code never seems to return the values of the array to Promise.all
and just returns unresolved promises.
Here's the reproduced code-
function func1(data) {
return new Promise(function(resolve, reject) {
var arr1 = data.split(". ");
var another_arr = arr1.map(function(sent) {
fetch('https://pokeapi.co/api/v2/pokemon/1/')
.then(function(response) {
return response.json();
})
})
resolve(Promise.all(another_arr))
})
}
function func_main() {
var some_string = "This is a sentence. This is another sentence."
this.func1(some_string).then(function(value) {
console.log(value)
})
}
func_main()
https://jsbin.com/vemasopojo/edit?js,console
Output: [undefined, undefined]
How do I ensure that the array is completely resolved before moving on to print it?