innovationacademy-kr / 42seoul-info

42 Cadet 정보를 확인할 수 있는 프로젝트

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

중복 코드 리팩토링

kenu opened this issue · comments

commented

방안 1

  • Promise.all 로 중복된 코드를 없애고, 병렬로 request 요청하는 방안
  • setTimeout도 clear해주어야한다.
const timeout = new Promise((resolve, reject) => {
  setTimeout(() => resolve(‘timeout’), 1000);
});

  Promise.all([axios.get(uri, headers), get42UserCoalition(username, headers), timeout])
    .then((data)=>{
        const one = {…data[0], 'coalition’: data[1]}
        ObjectUtils.calcDiff(one.projects_users, 'marked_at');
        await User.save(one);
        res.render('user', { 
          user: one, 
          updatedAt: dayjs().format('YYYY/MM/DD HH:mm:ss'), 
          dayjs 
        });

    })
   .catch(e=>{
        const error = new Error(e.message);
        console.log(e.message);
        error.status = e.response.status;
        if (error.status === 401) {
          res.redirect('/login/42');
          return;
        }
        next(error);
   })
  • 결과
Request failed with status code 429
  • (추측) 42 API에서 미지원. 429 => 틀렸다.
  • axios.all()로 보냈을때 정상적으로 받음 (settimeout 문제로 추측)

방안 2

  • 함수로 리펙토링
commented

@hochan222
sonarqube 확인 필요
참고: https://github.com/kenu/okdevtv

soarqube 탐방기

soarqube 실행

  • 전체 현황

  • if 문 curly brace 제거 => 오.. 오류를 잡아냅니다!!?!

  • if 문 curly brace 추가

  • 그외 코드줄수도 체크해주는 우리의 soarqube
    image
commented

LGTM!