본문 바로가기
카테고리 없음

js Promise 간단 사용법

by extrmk 2024. 11. 27.
function task() {
  return new Promise((resolve, reject) => {
    if(parseInt(Math.random()*10)%2==0) resolve('성공데이터');
    else reject('실패데이터');
  });
}

task()
.then(successData => {
  console.log(successData)
})
.catch(failedData => {
  console.error(failedData);
});

task()
.then(successData => {
  console.log(successData);
}, failedData => {
  console.error(failedData);
});