I need to iterate over a Map object - to make it realistic - lets say it is a collection of Polygons (Polygons is represented by a collection of coordinates - lat and long). Every item in the Map has a collection of items (coordinates). With every item (coordinate) in the Map (polygon), I need to do some asynch action that return a Promise. After the promise (return a string) collection is being completed - I would like to push it into an array with a preceding word of "POLYGON...". As all of this ends (the iteration over the Map) - I have an array of "Polygons" and their coordinates. Then I would like to do something with this array. The problem is that the Map iteration ended without taking into consideration the promises in it , meaning - the array that should be populated by the promises in the iteration is undefined when the iteration completed. See my code:
//Polygons is the Map object
let polygonsarray : [] = []
let strPolygonArray : string;
for (let [key, value] of this.Polygons.items.entries())
{
if (value.PolygonItem)
{
Promise.all(value.PolygonItem.map(p =>
{
return someAsychActionThatReturnPromise(p).then(res => res)
})).then(data =>
{
polygonsarray.push("POLYGON(" + data + ")"));
}).catch(err => return throwError(err));
}
}
//polygonsarray is still empty in this stage
strPolygonArray = polygonsarray.join();