1

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
  • 6,293
  • 12
  • 67
  • 141

2 Answers2

2

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
  • 112,247
  • 30
  • 221
  • 334
-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
  • 503
  • 4
  • 21