函数递归
Recursion on function
我正在开发一个装饰器来将中间件添加到 AWS lambda,这里是源代码。我在这方面的灵感来自调解器的源代码(具体来说,class link 中的“RequestHandlerWrapperImpl”)。所以我拿了 C# 代码并试图将它传递给 Typescript。简化代码是这样的。
// Used Types
type HandlerDelegate = () => Promise<APIGatewayProxyResult>;
type Handler = (event: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult>
type Middleware = (event: APIGatewayProxyEvent, next: HandlerDelegate) => Promise<APIGatewayProxyResult>;
// Middlewares
const first = (event: APIGatewayProxyEvent, next: HandlerDelegate) =>{
console.log("first in")
const response = next();
console.log("first out")
return response;
};
const second = (event: APIGatewayProxyEvent, next: HandlerDelegate) =>{
console.log("second in")
const response = next();
console.log("second out")
return response;
};
// Executable logic
const read = forHandler(async () : Promise<APIGatewayProxyResult>
=> APIResponse.createSuccess(HttpStatusCode.OK, "handler"))
.addMiddleware(first)
.addMiddleware(second)
.finish;
// finish method
const finish = (event: APIGatewayProxyEvent) : Promise<APIGatewayProxyResult> => {
let result: HandlerDelegate = () => handler(event);
for (let i = 0; i < middlewares.length; i++) {
result = () => middlewares[i](event, result);
}
return result();
}
预期的日志是这样的:
"first in"
"second in"
"handler"
"second in"
"first in"
但是,这是,直到溢出异常
"second in"
"second in"
"second in"
"second in"
"second in"
"second in"
"second in"
"second in"
谁能解释一下为什么会这样?我认为是因为在
行
result = () => middlewares[i](event, result);
我完全替换了结果变量(在“两侧”)。因此,它不是递归的,而是循环的。另外,我怎样才能让它按预期工作?
解决方案修改了finish函数,改为:
const finish = (event: APIGatewayProxyEvent) : Promise<APIGatewayProxyResult> => {
let result: HandlerDelegate = () => {
return handler(event);
};
for (let i = 0; i < middlewares.length; i++) {
let resultCallback = result;
result = () => {
return middlewares[i](event, resultCallback);
};
}
return result();
}
有了这个,结果函数就不会被覆盖,我们让管道工作
我正在开发一个装饰器来将中间件添加到 AWS lambda,这里是源代码。我在这方面的灵感来自调解器的源代码(具体来说,class link 中的“RequestHandlerWrapperImpl”)。所以我拿了 C# 代码并试图将它传递给 Typescript。简化代码是这样的。
// Used Types
type HandlerDelegate = () => Promise<APIGatewayProxyResult>;
type Handler = (event: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult>
type Middleware = (event: APIGatewayProxyEvent, next: HandlerDelegate) => Promise<APIGatewayProxyResult>;
// Middlewares
const first = (event: APIGatewayProxyEvent, next: HandlerDelegate) =>{
console.log("first in")
const response = next();
console.log("first out")
return response;
};
const second = (event: APIGatewayProxyEvent, next: HandlerDelegate) =>{
console.log("second in")
const response = next();
console.log("second out")
return response;
};
// Executable logic
const read = forHandler(async () : Promise<APIGatewayProxyResult>
=> APIResponse.createSuccess(HttpStatusCode.OK, "handler"))
.addMiddleware(first)
.addMiddleware(second)
.finish;
// finish method
const finish = (event: APIGatewayProxyEvent) : Promise<APIGatewayProxyResult> => {
let result: HandlerDelegate = () => handler(event);
for (let i = 0; i < middlewares.length; i++) {
result = () => middlewares[i](event, result);
}
return result();
}
预期的日志是这样的:
"first in"
"second in"
"handler"
"second in"
"first in"
但是,这是,直到溢出异常
"second in"
"second in"
"second in"
"second in"
"second in"
"second in"
"second in"
"second in"
谁能解释一下为什么会这样?我认为是因为在
行result = () => middlewares[i](event, result);
我完全替换了结果变量(在“两侧”)。因此,它不是递归的,而是循环的。另外,我怎样才能让它按预期工作?
解决方案修改了finish函数,改为:
const finish = (event: APIGatewayProxyEvent) : Promise<APIGatewayProxyResult> => {
let result: HandlerDelegate = () => {
return handler(event);
};
for (let i = 0; i < middlewares.length; i++) {
let resultCallback = result;
result = () => {
return middlewares[i](event, resultCallback);
};
}
return result();
}
有了这个,结果函数就不会被覆盖,我们让管道工作