I would like to calculate how long an async function (async
/await
) is taking in JavaScript.
One could do:
const asyncFunc =
Any already queued microtask will be fired first, and their execution time will be taken into account.
Yes, and there's no way around that. If you don't want to have other tasks contribute to your measurement, don't queue any. That's the only solution.
This is not a problem of promises (or async function
s) or of the microtask queue specifically, it's a problem shared by all asynchronous things which run callbacks on a task queue.