node and Error: EMFILE, too many open files

前端 未结 18 1842
终归单人心
终归单人心 2020-11-28 17:58

For some days I have searched for a working solution to an error

Error: EMFILE, too many open files

It seems that many people have the same proble

18条回答
  •  天命终不由人
    2020-11-28 18:31

    For anyone that might still be looking for solutions, using async-await worked fine for me:

    fs.readdir(, async (err, filenames) => {
        if (err) {
            console.log(err);
        }
    
        try {
            for (let filename of filenames) {
                const fileContent = await new Promise((resolve, reject) => {
                    fs.readFile(, 'utf-8', (err, content) => {
                        if (err) {
                            reject(err);
                        }
                        resolve(content);
                    });
                });
                ... // do things with fileContent
            }
        } catch (err) {
            console.log(err);
        }
    });
    

提交回复
热议问题