.js和.ts非常新,因此也适用于express.js。我有以下几行代码:
private post = async ( request: express.Request, response: express.Response, next:express.NextFunction) =>
{
await this.myfunction(next) ? console.log("Do nothing") : console.log("end function/request/response");
await this.anotherfunction();
}
我的功能如下:
private myfunction = async (next: express.NextFunction): Promise<boolean> => {
let temp: boolean = false;
if(temp){
return true;
} else {
next(
new HttpException(404, "just an example error")
);
return false;
}
HttpException函数的构建方式如下:https://wanago.io/2018/12/17/typescript-express-error-handling-validation/
我的问题: 当我触发发布功能时,将执行this.myfunction并将在控制台中打印“功能/请求/响应”。之后,将执行this.anotherfunction()。但是我想要,如果我从this.myfunction返回的结果是错误的,那么this.anotherfunction()将不会被执行,并且传入的请求将被停止/取消,因为我已经在this.myfunction()< / p>
我该怎么做?
答案 0 :(得分:0)
你不能简单地做吗?
const result = await this.myfunction(next);
if(result) {
await this.anotherfunction();
}
最好在末尾发送res.send
。