Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to see the number of promises in the event loop?

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.

like image 632
Dr. Light Avatar asked Dec 20 '25 16:12

Dr. Light


1 Answers

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.

like image 190
Marcos Casagrande Avatar answered Dec 23 '25 05:12

Marcos Casagrande