我有一个接口:
interface ICustomer {
id: number,
first_name: string
}
async consultApi( id: number ): Promise<any> {
const customer = await SomeExternalApi.getCustomer(id);
}
会永远回报你的承诺。据怀疑,我不想要承诺,我想要“SomeExternalApi.getCustomer”方法中类似散列的对象,因此我需要另一个异步函数来“解决”承诺:
async getCustomer( id: number ): Promise<any> {
const customer = await consultApi(id: number );
}
我怎么能做这样的功能:
getCustomer( id: number ): ICustomer {
const customer = consultApi(id: number );
}
我可以尽量避免使用所有的“异步”函数,然后编写如下代码:
return Promise.then(response => response.json)
在一个非异步函数中,但我敢打赌,问题是我缺乏TS方面的专业知识。总之,TS开发人员如何使用“异步”函数来获取“已解决”的数据并返回一个已实现的接口,而不是承诺?