Réécrire ce code avec async
On modifiera la lig.2 par const readline = require('readline').promises;
- const fs = require('fs');
- const readline = require('readline');
- const { stdin: input, stdout: output } = require('process');
- const rl = readline.createInterface({ input, output });
- rl.question('give some file names? ', (f) => {
- const filenames = f.split(" ");
- let counts = filenames.map(f => {
- try {
- const data = fs.readFileSync(f, { encoding: 'utf8' });
- return `${f}: ${data.split('\n').length} lines`;
- } catch (err) {
- return `${f}: couldn't read file`;
- }
- });
- console.log(counts.join('\n'));
- rl.close();
- });