🏠 back to Observable

TypeError: Convert is not a constructor

I was trying to use the standard example from ansi-to-html npm packae.

var Convert = require('ansi-to-html');
var convert = new Convert();

console.log(convert.toHtml('\x1b[30mblack\x1b[37mwhite'));

And while trying to wrap the example into an Observable Cell, I’ve got the TypeError: Convert is not a constructor. The Cell.

ansitohtml = {
  let Convert = require('https://bundle.run/ansi-to-html@0.6.14');
  let convert = new Convert();
  return convert
}

The Error.

The error here happens because Observable require is asynchronous (returns a promise) and constructors in JavaScript are not - new operation doesn’t understand what a promise is. The solution is to await until the promise resolves.

ansitohtml = {
  let Convert = await require('https://bundle.run/ansi-to-html@0.6.14');
  let convert = new Convert();
  return convert
}

ansitohtml.toHtml('\x1b[94mRunning number: \x1b[96mtest_line\e[0m')

Or maybe Observable language can make new operation understand asynchronous objects as well?

Hi @abitrolly,

I am not sure if this will respond to your question in any way, but I tried re-writing the ‘standard example’ in more of an OberservableHQ manner, and it seems to work just fine:

Essentially, I just used the Observable ‘require’ and followed along the variable names.

If this wasn’t it, I hope others will respond.

Wishing you the very best,

Aaron

This works, because require and new are located in separate cells. I tried to set ansitohtml function in a single cell to keep notebook simple.

1 Like

My first notebook :blush:

2 Likes

Nice work solving your own question!

1 Like

Nice abitrolly Glad you were able to figure it out and congrats on your first notebook!

1 Like