큰 중첩의 재귀함수콜 방식을 콜스택 초과문제 없이 사용해보기
테스트한 코드는 다음과 같다
bGV0IOu5hOuPmeq4sO2VqOyImCA9IGZ1bmN0aW9uICh2LCBhKSB7CiAgIGEgPSAoYSA/IGEgOiAxKSArIHY7CiAgIHYgPiAxID8gcXVldWVNaWNyb3Rhc2soKCkgPT4g67mE64+Z6riw7ZWo7IiYLmJpbmQodGhpcykoLS12LCBhKSkgOiB0aGlzKGEpOwp9OwpsZXQg64+Z6riw7ZWo7IiYID0gZnVuY3Rpb24gKHYsIGEpIHsKICAgYSA9IChhID8gYSA6IDEpICsgdjsKICAgdiA+IDEgPyDrj5nquLDtlajsiJguYmluZCh0aGlzKSgtLXYsIGEpIDogdGhpcyhhKTsKfTsKbGV0IOyLpO2Wieq4sCA9IGFzeW5jICjtmp/siJgsIOyCrOyaqe2VoO2VqOyImCkgPT4gewogICB0cnkgewogICAgICBsZXQgcmVzdWx0ID0gYXdhaXQgbmV3IFByb21pc2UociA9PiDsgqzsmqntlaDtlajsiJguYmluZChyKSjtmp/siJgpKTsKICAgICAgX2NvbnNvbGUubG9nKGAke+yCrOyaqe2VoO2VqOyImC5uYW1lfeydmCDqsrDqs7wgJHtyZXN1bHR9YCk7CiAgIH0gY2F0Y2ggKGUpIHsKICAgICAgX2NvbnNvbGUubG9nKGAke+yCrOyaqe2VoO2VqOyImC5uYW1lfSDsgqzsmqkg7KSRIOyXkOufrOuwnOyDnSAke2UudG9TdHJpbmcoKX1gKQogICB9Cn0K7Iuk7ZaJ6riwKDk5OTk5LCDruYTrj5nquLDtlajsiJgpOwrsi6TtlonquLAoOTk5OTksIOuPmeq4sO2VqOyImCk7
let 비동기함수 = function (v, a) {
a = (a ? a : 1) + v;
v > 1 ? queueMicrotask(() => 비동기함수.bind(this)(--v, a)) : this(a);
};
let 동기함수 = function (v, a) {
a = (a ? a : 1) + v;
v > 1 ? 동기함수.bind(this)(--v, a) : this(a);
};
let 실행기 = async (횟수, 사용할함수) => {
try {
let result = await new Promise(r => 사용할함수.bind(r)(횟수));
_console.log(`${사용할함수.name}의 결과 ${result}`);
} catch (e) {
_console.log(`${사용할함수.name} 사용 중 에러발생 ${e.toString()}`)
}
}
실행기(99999, 비동기함수);
실행기(99999, 동기함수);