I have the following async function
export default async function getUserNames(id: string[]): Promise<string[]> {
let userNames: string[] = [];
// We do some stuff here like calling a service, etc...
return userNames;
}
On a different typescript file, I am importing the getuserNames function and trying to call it like this:
const promiseResult = getUserNames(idList)
.then(result => {
return result;
})
.catch(error => {
return undefined;
});
if (promiseResult) {
// Do something else here.
}
However, the promiseResult type is of Promise instead of string[] which is what I am expecting. How can I call the getuserNames function and when it is done, the actual string[] is returned to the promiseResult variable?
EDIT Is it accepted to do something like this?
let varB: string[];
const promiseResult = getUserNames(idList)
.then(result => {
varB = result;
})
.catch(error => {
varB = undefined;
});
if (varB) {
// Do something else here.
}
Lastly, please notice that the function that calls getUserNames is not defined as async and I can't change that.