exercice

 Réécrire ce code avec async

On modifiera la lig.2 par const readline = require('readline').promises;

  1. const fs = require('fs');
  2. const readline = require('readline');
  3. const { stdin: input, stdout: output } = require('process');

  4. const rl = readline.createInterface({ input, output });

  5. rl.question('give some file names? ', (f) => {

  6.   const filenames = f.split(" ");
  7.   let counts = filenames.map(f => {
  8.     try {
  9.       const data = fs.readFileSync(f, { encoding: 'utf8' });
  10.       return `${f}: ${data.split('\n').length} lines`;
  11.     } catch (err) {
  12.       return `${f}: couldn't read file`;
  13.     }
  14.   });
  15.   console.log(counts.join('\n'));


  16.   rl.close();
  17. });