2

I'm using the q library for promises. I have the following code

Q.all([thumbnailPromise, createSnapshotPromise]).spread((thumbnailRep, snapshotRep) => {

How do I determine how long each promise took? Specifically, how long thumbnailPromise and createSnapshotPromise took separately?

Note that I want to keep the promises running in parallel.

Thanks!

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165

2 Answers2

3

I guess you could write a function that wraps your promise:

const timedPromise = async (promFac) => {
    const start = performance.now();
    const returnValue = await promFac();
    return {
        value: returnValue,
        elapsed: performance.now() - start;
    }
}

and use it like this:

Q.all([
    timedPromise(() => thumbnailPromise) , 
    timedPromise(() => createSnapshotPromise)])
  .spread((thumbnailRep, snapshotRep) => {
    console.log(`thumbnailProm took ${thumbnailRep.elapsed}, returned ${thumbnailRep.value}`);
  })

Untested.

spender
  • 117,338
  • 33
  • 229
  • 351
-1

You can use console.time(); MDN reference Another option would be to use hrtime.

Promises are functions, if you don't have access directly to the functions, you can wrap them in one.

Király István
  • 599
  • 5
  • 24