I have a somedataMap : Map<string, Promise<SomeData>>
that I would like to populate.
I also have an async function async function getSomeData(someIdentifier:string): Promise<SomeData>
, that I would like use as the values in that map
Obviously if I do something like somedataMap.set("someId", getSomeData("someId"))
, then the code in getSomeData
is called, which I don’t want to do until maybe later. What is the best way to wrap/closure the getSomeData
function as a Promise so I can store it or pass it to other functions?
2
Answers
You can use either
OR
use it as it is, and later, when you want to receive value from Promise, do the following:
Async functions always return promises. You don’t need to wrap it. Just call it.