Using awaits with fs.watch() in node.js

Viewed 605

I have some code that talks connects to a redis and returns a promise like:

async connectionToRedisClient() {
    return await this.connect('connect')
    .then(() => {
        log.info('Connected');
    })
    .catch(() => log.error('Error connecting to redis'));
}

Which is later used to wait for a successful connection before proceeding with an algorithm:

await connectionToRedisClient()

This all works fine on its own, and has done for some time now - however, I am adding the fs.watch function to trigger this code when a new file appears in a directory.

The fs.watch code works fine, and attempts to execute the above code, but suddenly stops at the await and does not complete. My debugging leads to this code and only this code.

If I remove the redis code then the code completes fine. Is there a problem using awaits in fs.watch? or do I have some mix up with sync & async code here? Keep in mind that if I stop using fs.watch, the redis code is fine, so the problem isn't directly related to my redis code.

See my fs.watch code for reference:

fs.watch('./targets', async (event, filename) => {
    console.log('event is: ' + event);
    if (filename === 'file.txt') {
       await main();
    } else {
        console.log('Incorrect file passed');
    }
});
0 Answers
Related