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

前端 未结 7 724
一向
一向 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:26

    Here is the solution:

    // Wait for file to exist, checks every 2 seconds
    function getFile(path, timeout) {
        const timeout = setInterval(function() {
    
            const file = path;
            const fileExists = fs.existsSync(file);
    
            console.log('Checking for: ', file);
            console.log('Exists: ', fileExists);
    
            if (fileExists) {
                clearInterval(timeout);
            }
        }, timeout);
    };
    

提交回复
热议问题