基本上,我所拥有的是一个包含类MyClass的文件,该类随后包含其中的一些其他功能,而该文件由一个运行良好的单独文件调用。但是,我想在同一类的外部在同一个文件中添加一个异步函数,然后从该类内部的函数之一调用/执行它。它看起来像这样:
async function myAsync(){
//do stuff here
}
// Main class
class MyClass {
firstFunction() {
//call async function myAsync here
}
}
// Exports class back to other file which runs it
module.exports = MyClass;
// file1
async function myAsync(){
}
class MyClass {
async firstFunction() {
return await myAsync();
}
}
module.exports = MyClass;
// file2
const my = new MyClass();
(async()=> {
await my.firstFunction();
})()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句