如何导出asynchronous函数?
以下是我的代码 –
abc.js
class abc { async foo1() { // do something return result; } async foo2() { // do something return result; } module.exports = abc }
另外,file.js
const abc = requir(./abc); abc.foo1(); // this results in error // TypeError : abc.foo1 is not a function
我应该怎么做?
几点。 按照惯例,类名以大写字母开头。 你的问题与asynchronous函数无关。 你有2个select来解决这个问题。 第一个select是让你的function是静态的。 那么你可以直接使用它没有实例。 第二个选项只是以不同的方式调用:首先实例化类以获取实例,然后在此实例上调用您的方法。 而且请记住,await关键字只能在其他asynchronous函数中使用。 如果你想处理promise,你需要等待关键字,由asynchronous函数返回(它返回result
承诺, result
不是result
)