Skip to content

inspector: change api to promise-like

The old way is very easy to write callback hell hard to read and maintain

old way

const inspector = require('inspector');
const fs = require('fs');
const session = new inspector.Session();
session.connect();

session.post('Profiler.enable', () => {
  session.post('Profiler.start', () => {
    // Invoke business logic under measurement here...

    // some time later...
    session.post('Profiler.stop', (err, { profile }) => {
      // Write profile to disk, upload, etc.
      if (!err) {
        fs.writeFileSync('./profile.cpuprofile', JSON.stringify(profile));
      }
    });
  });
});

new way

New way should be something like this:

const inspector = require('inspector')
const session = new inspector.Session()
session.connect()
const wait = n => new Promise(resolve => {
    setTimeout(() => resolve(), n)
})
session
  .post('Profiler.enable')
  .then(() => session.post('Profiler.start'))
  .then(wait(2000))
  .then(() => session.post('Profiler.stop'))
  .then(console.log)
  .catch(console.error)

cc @nodejs/inspector I don't know whether this should create a issue to discuss it.

I have not touch the test case and doc (luckily it's still an experimental api).

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Merge request reports

Loading