我编写了一个函数来处理异步函数引发的异常,例如:
protected safeAsync(f : e.RequestHandler) : e.RequestHandler {
/* how to detect if `f` is an async function here ?? */
const binded = f.bind(this);
return async function innerCall(req : e.Request, res : e.Response, next : Function) {
try {
return await binded(req, res, next);
} catch (err) {
log.debug(err);
return next(err);
}
}
}
Run Code Online (Sandbox Code Playgroud)
该参数f应该是一个异步函数,例如
public async signupPost(req : e.Request, res : e.Response, next : Function) {/*...*/}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
在 中safeAsync,有没有办法检测是否f是异步函数,以便我可以记录对 的意外调用safeAsync?
由于这是运行时检查,因此您应该问的问题是 JavaScript 是否提供了一种方法来检查函数是否异步。答案是否定的,没有万无一失的方法来判断一个函数是否异步,甚至可能两者都是。
| 归档时间: |
|
| 查看次数: |
1261 次 |
| 最近记录: |