Vassil_Iliev

async

Jun 21st, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { FileHandler } from './services/file-handler.js';
  2.  
  3. const reader = new FileHandler();
  4.  
  5. reader
  6.     .read('./data/tasks.all.json')
  7.     .then(data => JSON.parse(data))
  8.     .then(data => {
  9.         const done = data.tasks.filter(x => x.status === 'done')
  10.         const pending = data.tasks.filter(x => x.status === 'pending')
  11.  
  12.         const promise1 = reader.write('./data/tasks.done.json', JSON.stringify(done, null, 2));
  13.         const promise2 = reader.write('./data/tasks.pending.json', JSON.stringify(pending, null, 2));
  14.  
  15.         Promise.allSettled([promise1, promise2]).then(errors => {
  16.             const error = errors.filter(x => x.status === 'rejected').map(x => x.reason)
  17.  
  18.             if (error.length > 0) {
  19.                 console.log('There is an Error/s, check the log.')
  20.             }
  21.  
  22.             reader.read('./logs/default.log')
  23.                 .then(data => {
  24.                     reader
  25.                         .write('./logs/default.log', data + '\n' + error.join('\n'))
  26.                         .catch(err => console.log(err));
  27.                 })
  28.                 .catch(err => console.log(err))
  29.         }).catch(err => console.log(err));
  30.  
  31.     }).catch(err => {
  32.         reader
  33.             .read('./logs/default.log')
  34.             .then(data => {
  35.                 reader
  36.                     .write('./logs/default.log', data + '\n' + err)
  37.                     .catch(err => console.log(err));
  38.             }).catch(err => console.log(err))
  39.  
  40.         console.log('There is an Error thrown, check the log.')
  41.     });
  42.  
Advertisement
Add Comment
Please, Sign In to add comment