Reputation: 444
Basically like the title says, is there a way to see how many promises currently exist within the event loop from within the JavaScript program? Preferably on Deno.
Upvotes: 3
Views: 299
Reputation: 40404
Deno v1.26 added an internal API that can be used to track promises.
Deno.core.setPromiseHooks(init, before, after, resolve);
Deno.core.setPromiseHooks(
// init
(promise, parentPromise) => {
console.log('init', promise, parentPromise);
},
// before
promise => {},
// after
promise => {},
// resolve
promise => {
console.log(promise, 'resolved');
}
);
For a detailed usage check the following Deno test:
https://github.com/denoland/deno/blob/main/cli/tests/unit/promise_hooks_test.ts
NOTE: The API is undocumented and may change in the future.
Upvotes: 1