【发布时间】:2021-10-19 00:10:52
【问题描述】:
我有一个 Class 构造函数,我想用一个函数包装其中一个方法。这样做的语法是什么?
// function that wraps around the method
const wrapCatch = (fn) => async (...args) =>
fn(args).catch(err => throw new Error(err));
class myClass {
constructor(fn1) {
this.fn1 = fn1;
}
async fnToBeWrapped () {
return await this.fn1();
}
}
基本上是在尝试这样做,但对于类方法...
const fnToBeWrapped = wrapCatch(async () => {
// ...
});
【问题讨论】:
-
仅供参考
fn(args)应该是fn(...args)
标签: javascript class methods constructor