请注意,以下代码不是实际代码。有很多是为了勇敢而省略的。
我正在使用对打字稿做出反应,我的所有组件都是功能组件。
我有以下功能,就像
export const filterData = (data) => {
// do lots of filtering on data, does not make any api calls.
// only do Array.filter, map etc to remove unwanted data
};
该函数在单独的 utils 文件中定义并导入我的反应组件中。我使用它如下
//Fetch data using hook from API
const receivedDataFromAPI = useDetails(detailsParams);// Data is already received here. Verified that data is coming from network tab and console log
const cleanedData = allFilter(receivedDataFromAPI);
const allFilter = async (receivedData: IData) => {
const data = await filterData(receivedData);
// other code
}
早些时候我没有将它与 async await 一起使用,但随后执行并没有等待 filterData 返回结果。在做了很多试验和错误之后,我来到异步等待,代码现在工作正常。问题是上面的模式确定还是我需要使 filterData 函数异步?