Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

songkick/promise-stopwatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

About

This software is not maintained. Measure a Promise resolution duration using Performance API with fallback to `new Data()`

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •