Skip to content

Instantly share code, notes, and snippets.

@mcsf
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save mcsf/b9021539ec5ef8dfb87d to your computer and use it in GitHub Desktop.

Select an option

Save mcsf/b9021539ec5ef8dfb87d to your computer and use it in GitHub Desktop.
Timing async calls with promises
const now = () => new Date().getTime();
const timed = (promise, key = 'delta') => {
const start = now();
return promise.then(data => ({
...data,
[key]: now() - start
}));
}
const test = () => {
const myFakeAsync = () => new Promise(res => setTimeout(res.bind(null, { message: 'hello'}), 1337));
return timed(myFakeAsync());
}
// use `babel-node --stage=0`
test().then(console.log.bind(console));
// --> { message: 'hello', delta: 1341 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment