흔히 알고 쓰지만 용어 정리겸 남겨봅니다.
**higher-order function that takes a function as an argument:
function higherOrderFunction(callback) {
console.log("The higher-order function is running.");
callback();
}
// first order function
function callbackFunction() {
console.log("The callback function is running.");
}
higherOrderFunction(callbackFunction);
**higher-order function that returns a function:
function higherOrderFunction() {
return function () {
console.log("The returned function is running.");
};
}
용어정리2
**Parameter 매개변수 함수와 메서드 입력 변수(Variable) 명
function plus(num1, num2) {
return num1 + num2;
}
**Argument 전달인자, 인자 함수와 메서드의 입력 값(Value)
plus(10, 20);
이번에 async 함수 전체를 function의 인수로 가져와서 사용했는데 굉장히 어색했다ㅠㅠ.
exports.tryCatch = (controller) => async (req, res, next) => {
try {
await controller(req, res);
} catch (error) {
return next(error);
}
};
위 예시는 HOF(Higher Order Function) 과 CBF(Callback funciton) 을 비교 할수 있다.
(controller) => 부분은 function을 가져와 실행 시킨다 그러므로 HOF이다. 그리고 await controller는 promise를 리턴시키는 CBF를 볼수있다.