Nodejs check file exists, if not, wait till it exist

前端 未结 7 744
一向
一向 2021-02-05 08:54

I\'m generating files automatically, and I have another script which will check if a given file is already generated, so how could I implement such a function:

f         


        
7条回答
  •  无人共我
    2021-02-05 09:17

    function holdBeforeFileExists(filePath, timeout) {
       timeout = timeout < 1000 ? 1000 : timeout;
       return new Promise((resolve)=>{  
           var timer = setTimeout(function () {
               resolve();
           },timeout);
    
           var inter = setInterval(function () {
           if(fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()){
               clearInterval(inter);
               clearTimeout(timer);
               resolve();
           }
         }, 100);
      });
    }
    

提交回复
热议问题