DEV Community

Andy Allison
Andy Allison

Posted on

 

NodeJs Console Progress Bar

Overview

I'm not the greatest at writing these things as in-depth tutorials so this is pretty much a very simple example of how I've been using the progress bar in node to trach and illustrate progress when I've been processing large amounts of data and files.

The idea behind this is to show a script which will set a progress bar. It will then incrementally progress the bar (see what I did there ;) ) as the work is done on the elements.

The library used is https://www.npmjs.com/package/progress

// npm 
$ npm install progress
// Yarn
$ yarn add progress
Enter fullscreen mode Exit fullscreen mode

The good stuff

import ProgressBar from 'progress';

// Simple method that pretends it's doing some work
const doSomeStuff = async () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('Waiting');
      resolve('done');
    }, 1000);
  })
}

// main method that is doing the orchestration of all the other work
const doMeSomeImports = async () => {
  const itemsToProcess = [{id:1},{id:2},{id:3},{id:4},{id:5}];
  const bar = new ProgressBar('-> Processing [:bar] :percent :etas', {
    total: itemsToProcess.length * 2,
    width: 30,
  });
  for (const item of itemsToProcess) {
    bar.tick(1);
    await doSomeStuff();
    bar.tick(1);
  }
}

doMeSomeImports()
  .catch((e) => console.error(e))
  .then(() => console.log('Complete!'))

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

50 CLI Tools You Can't Live Without

>> Check out this classic DEV post <<