コードレビュー中級約18分

レビュー: async/awaitの問題

typescriptasyncerror-handlingpromises

コードレビュー課題

以下のTypeScriptコードをレビューしてください。

背景

複数のAPIを呼び出してデータを取得する機能です。開発者はasync/awaitを使用していますが、エラーハンドリングやパフォーマンスに問題があります。

レビュー観点

  • エラーハンドリング
  • 非同期処理のパフォーマンス
  • Promise の適切な使用
  • 例外の伝播

あなたの回答

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+クリックで範囲選択)

最低100文字必要です

0

コメントを追加し、サマリーを100文字以上入力してください

模範解答

回答を送信するか、「表示する」をクリックすると模範解答が表示されます。