Iterating over a Map object involves processing a collection of Polygons, where each Polygon is represented by a set of coordinates including latitude and longitude. The goal is to perform an asynchronous action on each coordinate within the Map (Polygon) that returns a Promise. Upon completion of the Promise, which results in a string, it should be added to an array with "POLYGON..." preceding it. Once all iterations over the Map are finished, there will be an array containing "Polygons" along with their respective coordinates. This array will then be used for further actions. However, a problem arises as the Map iteration completes without accounting for the Promises within it. This means that the array intended to store the results of these Promises remains undefined when the iteration finishes. The code snippet displays this process:
//Polygons represents 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 => throwError(err));
}
}
//the polygonsarray still remains empty at this stage
strPolygonArray = polygonsarray.join();