0

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)

Guy E
  • 1,775
  • 2
  • 27
  • 55
  • Define: "isnt working"? An error? Not returning what you expect? Soemthing else? – Jamiec Jan 13 '22 at 13:35
  • 2
    `doSomething(api, param1, param2)` is very different from `doSomething([{param1:par1, param2:par2}])` – Bergi Jan 13 '22 at 13:38
  • Are you calling it the same way? In de testing function you call the api function with one array containing one object, while in the static function you pass it three separate arguments. – RobCo Jan 13 '22 at 13:38
  • What api is it specifically? Please share the actual code, and a link to the api documentation. It looks like you might even be able to pass an array right away, and not even need the `polygonItems.map()` call. – Bergi Jan 13 '22 at 13:39
  • @Bergi - I fixed it - one is a wrapper to the other - I send the api object as a parameter, and then apply the original one using it and the 2 other parametsr – Guy E Jan 13 '22 at 13:44
  • @Bergi - I don't have the actual code - I just have the test code that works with the "then" keyword - testingDoSomething – Guy E Jan 13 '22 at 13:46
  • @Bergi - Sorry again - I fixed the wrapper function – Guy E Jan 13 '22 at 13:48
  • @Bergi - "It looks like you might even be able to pass an array right away, and not even need the polygonItems.map() call" - it seems like you are right - I'll try it ! – Guy E Jan 13 '22 at 13:51

0 Answers0