Transitioning from coding in Clojure for the past two years to TypeScript has been an interesting journey. However, I've hit a bit of a roadblock today.
The issue lies with my interface:
interface ICustomer {
id: number,
first_name: string
}
I need a function that can consume an API, retrieve customer data, and return it as per the defined interface. The problem arises when dealing with "async" functions like:
async consultApi( id: number ): Promise<any> {
const customer = await SomeExternalApi.getCustomer(id);
}
This function always returns a promise, but what I actually want is the object from the "SomeExternalApi.getCustomer" method. So, I attempt to handle this by creating another async function:
async getCustomer( id: number ): Promise<any> {
const customer = await consultApi(id: number );
}
However, as expected, this also returns a promise instead of the desired customer data. How can I break out of this cycle?
One solution might be to avoid using async functions and try something like:
return Promise.then(response => response.json)
In a synchronous function. But, I suspect the root cause of the problem is my limited experience with TypeScript. How do TypeScript developers effectively use "async" functions to obtain resolved data and return it within a fulfilled Interface, rather than just a Promise?