Code ReviewIntermediate~18 min

Review: async/await Issues

typescriptasyncerror-handlingpromises

Code Review Task

Please review the following TypeScript code.

Background

This functionality calls multiple APIs to fetch data. The developer is using async/await but there are issues with error handling and performance.

Review Points

  • Error handling
  • Asynchronous processing performance
  • Proper use of Promises
  • Exception propagation

Your Answer

dataFetcher.tstypescript
async function fetchUserData(userId: number) {
const response = await fetch(`/api/users/${userId}`);
return response.json();
}
async function fetchUserPosts(userId: number) {
const response = await fetch(`/api/users/${userId}/posts`);
return response.json();
}
async function fetchUserComments(userId: number) {
const response = await fetch(`/api/users/${userId}/comments`);
return response.json();
}
async function getAllUserData(userId: number) {
// すべてのデータを取得
const user = await fetchUserData(userId);
const posts = await fetchUserPosts(userId);
const comments = await fetchUserComments(userId);
return {
user,
posts,
comments
};
}
export { getAllUserData };
行番号をクリックしてコメントを追加(Shift+クリックで範囲選択)

Minimum 100 characters required

0

Add comments and enter at least 100 characters in the summary

Model Answer

Submit your answer or click "Show" to view the model answer.