Last active
August 29, 2015 14:25
-
-
Save mcsf/b9021539ec5ef8dfb87d to your computer and use it in GitHub Desktop.
Timing async calls with promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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