Dr. Light
Dr. Light

Reputation: 444

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.

Upvotes: 3

Views: 299

Answers (1)

Marcos Casagrande
Marcos Casagrande

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

Related Questions