All files / src/chap05 ThrowResult.js

0% Statements 0/8
0% Branches 0/2
0% Functions 0/4
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                                             
import wrapPromise from "./wrapPromise";
 
// Promise의 상태를 관리하는 오브젝트를 가져온다.
const info = getInfo();
// Promise의 상태에 따라 결과를 표시하는 컴포넌트
export default function ThrowResult() {
  const result = info.get();
  return <p>{result}</p>;
}
 
// 비동기적으로 데이터를 취득하기 위한 함수
function getInfo() {
  return wrapPromise(new Promise((resolve, reject) => {
    // 2000밀리초 후 50% 확률로 성공/실패 메시지를 생성한다.
    setTimeout(() => {
      if (Math.random() > 0.5) {
        resolve('Succeeded!!');
      } else {
        reject('Error!!');
      }
    }, 2000);
  }));
}