중복 코드 리팩토링
kenu opened this issue · comments
- 진단
- 처방(리팩토링)
@hochan222
방안 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
- 함수로 리펙토링
@hochan222
sonarqube 확인 필요
참고: https://github.com/kenu/okdevtv
LGTM!