Promise 대신 async/await를 쓰면 더 편하다고 하는데 사용법을 알고 싶습니다.
에러 처리는 try-catch로 하면 되나요?
async 함수 안에서 await로 Promise 완료를 기다릴 수 있습니다.
async function getData() { try { const res = await fetch('/api/data'); const data = await res.json(); return data; } catch(err) { console.error('에러:', err); }}
then 체이닝보다 훨씬 읽기 쉽죠.
const [a, b] = await Promise.all([fetchA(), fetchB()]) 이렇게 Promise.all을 쓰면 병렬로 처리됩니다.
const [a, b] = await Promise.all([fetchA(), fetchB()])
댓글 4
async 함수 안에서 await로 Promise 완료를 기다릴 수 있습니다.
then 체이닝보다 훨씬 읽기 쉽죠.
const [a, b] = await Promise.all([fetchA(), fetchB()])이렇게 Promise.all을 쓰면 병렬로 처리됩니다.