Promise.all
은 JavaScript에서 제공하는 Promise 메서드로, 여러 개의 Promise를 병렬적으로 처리하고, 모든 Promise가 성공적으로 완료되었을 때 하나의 결과를 반환한다.
Promise.all([promise1, promise2, promise3])
.then((results) => {
console.log(results); // 모든 Promise의 resolve 결과가 배열로 반환
})
.catch((error) => {
console.error(error); // 하나라도 실패하면 해당 에러 출력
});
Promise.allSettled
을 사용하는 것이 적합하다.아래는 내가 이번 프로젝트에서 활용했던 코드이다.
const postsWithUserProfiles = await Promise.all(
posts.map(async (post) => {
const { data: userProfile, error: userProfileError } = await supabase
.from("user_profiles") // Supabase의 "user_profiles" 테이블에서
.select("id, username, profile_image_url") // 필요한 필드 선택
.eq("id", post.user_id) // 현재 post의 user_id와 일치하는 프로필 찾기
.single(); // 단일 결과만 반환
if (userProfileError) {
console.error(`Error fetching user profile for post ${post.id}:`, userProfileError);
return { ...post, userProfile: null }; // 에러가 있으면 프로필 없이 반환
}
return { ...post, userProfile }; // 성공하면 프로필 데이터를 병합
})
);
posts.map
:posts
배열의 각 요소(게시물)에 대해 비동기 작업(supabase.from
)을 실행한다.post
의 user_id
를 기준으로 "user_profiles" 테이블에서 데이터를 조회한다.Promise.all
:posts.map
은 각 게시물에 대한 비동기 작업(post.user_id
로 프로필 조회)을 Promise 배열로 반환한다.Promise.all
은 이 배열을 병렬로 처리하고, 모든 Promise가 완료되면 결과를 배열로 반환한다.postsWithUserProfiles
는 다음과 같은 구조를 가진 배열이다 [
{
id: 1,
title: "Post 1",
user_id: 101,
userProfile: {
id: 101,
username: "user_101",
profile_image_url: "url_101"
}
},
{
id: 2,
title: "Post 2",
user_id: 102,
userProfile: null // 프로필 가져오기 실패 시 null
}
]
Promise.all
을 사용해 병렬 처리를 수행하므로, 각 게시물마다 사용자 프로필을 순차적으로 가져오는 것보다 속도가 빠르다.async/await
와 함께 사용하여 가독성 높은 코드를 작성할 수 있다.posts
의 길이가 매우 길면 많은 비동기 요청이 한 번에 실행되므로 서버에 부하가 갈 수 있다.Promise.all
은 하나의 Promise라도 실패하면 전체가 실패로 간주된다. 하지만 이 코드는 개별적으로 error
를 처리하므로 전체 중단을 방지한다.
뿌라미수~ (약속해줘~)