繁体   English   中英

无法在 setInterval 函数中调用异步函数

[英]unable to call asynchronous function inside setInterval function

我想调用从setInterval函数内的服务类实例调用的异步函数。

        class async {

        constructor(public service: Service){}            

            async doSomeAsyncTask(){

               setInterval(await this.service.anotherAsyncTaskInTheServiceClass(),3000); 

       //want to call the inside of setInterval function repeatedly in every 3ms.       
      } 
    }
  }

setInterval 期望第一个参数是一个函数,所以我认为它应该是

setInterval(async () => { await this.service.anotherAsyncTaskInTheServiceClass() },3000); 

*感谢@Evariste的更正*

请务必将任何可能引发异常的操作包装在您的异步函数中,用作 try/catch 中setInterval / setTimer参数,以避免未处理的异常,请参阅Node.js 中的异步、承诺和计时器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM