promise-stopwatch Build Status Code Climate Test Coverage

Measure a Promise resolution duration using Performance API with fallback to new Data().

var stopwatch = require('@songkick/promise-stopwatch');
function resolveInOneSec() {
  return new Promise(function(resolve, reject){
    setTimeout(function(){
      resolve('yay');
    }, 1000);
  });
}

var settings = {}; // there is none available for now

stopwatch(settings)(resolveInOneSec)().then(function(response){
  var result = response.result;  // === 'yay'
  var duration = response.duration; // ~== 1000
  var settings = response.settings; // === {}

  console.log('Resolution took ' + duration + 'ms');

  return result; // return the result so you can handle it normally
});

function rejectInOneSec() {
  return new Promise(function(resolve, reject){
    setTimeout(function(){
      reject('nay');
    }, 1000);
  });
}

stopwatch(settings)(rejectInOneSec)().catch(function(response){
  var error = response.error;  // === 'nay'
  var duration = response.duration; // ~== 1000
  var settings = response.settings; // === {}

  console.log('Rejection took ' + duration + 'ms');

  throw error; // possibly re-throw the error so you can handle it normally
});

Options

None, for now, but it might come, so we keep this signature similar to other promise helper and be able to some without change the API.

See also

promise-stopwatch composes really well with the following promise helper: