Skip to content

Instantly share code, notes, and snippets.

@tappoz
Created August 30, 2017 14:24
Show Gist options
  • Save tappoz/74b8d6df7155ffb7b1862f8d0bc9e1aa to your computer and use it in GitHub Desktop.
Save tappoz/74b8d6df7155ffb7b1862f8d0bc9e1aa to your computer and use it in GitHub Desktop.
Javascript / Node.js tricks

Moment.js durations

var moment = require('moment-timezone');

var _timeConsumingFunc = () => {
  // wait a few seconds
  for (i = 0; i < 1000000000; i++) { 
    // do nothing
  }
}

var _promiseChainFunc = () => {
  var start = moment.tz('Europe/London');
  return Promise.resolve()
    .then(() => {
      return _timeConsumingFunc();
    })
    .then(() => {
      const duration = moment.tz('Europe/London').diff(start, 'seconds', true);
      console.log(`The elapsed time: ${duration}`);
    });
};

_promiseChainFunc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment