我对.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/
我的问题:当我触发我的post函数时,这个.my函数将被执行,并且"函数/请求/响应"将在控制台中打印出来。之后将执行this.anotherfunction((。但我希望,如果我从this.myfunction返回false,那么this.anotherfunction((将不会执行,并且我的传入请求将停止/取消,因为我已经在this.myfunction((中向我的最终用户发送了响应
我该怎么做?
你能不简单地做吗?
const result = await this.myfunction(next);
if(result) {
await this.anotherfunction();
}
最好在最后发送CCD_ 1。