This is a continuous question to this question: ("use promise in array items inside a Map object")
use promise in array items inside a Map object
I have an api that should return a promise. I would like to go over a Map Object items values - each value has a property ("polygonItems") that is a collection of objects representing polygons coordinates. I would like to iterate over the collection, and for each element apply my external api wrapper function (Utils.mydoSomething).
This is my main function:
async iterateOverItemsWithAsyncfunc() : Promise<string>
{
let data : string[] = [];
let strArra : string[] = await Promise.all(
Array.from(this.myPolygons.items.values(), async ({polygonItems}) : Promise<string> =>
{
let data = await Promise.all(polygonItems.map(pi => Utils.mydoSomething(api, pi.param1, pi.param2)));
return "Polygon:" + data.join();
})
);
return strArra.join(",");
}
My problem is with the Utils.mydoSomething function - it is a wrapper over an external api. This is how the external api is displayed in a test page, and it is working !
function testingDoSomething(par1, par2)
{
api.doSomething([{param1:par1, param2:par2}]).then(function(e)
{
showResult(e);
});
} i would like to use the part that return Promise, so I created my own function in Utils: I would like to execute the original function - it isn't working when running it from the main function shown above. I created alternative function that return a Promise, which works when running it in the main function:
Static async mydoSomething(api, par1, par2) : Promise<string>
{
return api.doSomething([{param1:par1, param2:par2}]); // NOT RETURNING VALUE
return new Promise(function(resolve, reject){resolve("My string result")}); //WORKING
}
what should be the problem (the external api function works when executing it as shown in the test page)