2

As far as I understand, phantomjs console is asynchronous. This means that this code will show a broken output:

console.log(very_long_text);
phantom.exit(0);

Only part of the very_long_text will be visible. The end of it will be lost, because of the asynchronous nature of console.log().

What is the workaround? I need to see the entire very_long_text.

Maybe this discussion is relevant: is node.js' console.log asynchronous?

ps. This is the best I can do now, but it's extremely ugly:

console.log(very_long_text);
setTimeout(function() { phantom.exit(0); }, 100);
Community
  • 1
  • 1
yegor256
  • 102,010
  • 123
  • 446
  • 597

1 Answers1

2

Are you sure that console.log run asynchronously ?

I can write 5MB to the console without any truncations. You can try another overload by using system module like this example :

require('system').stdout.write(very_long_text);
phantom.exit(0);

So console.log is synchronous but may run inside an asynchronous code (eg a callback) . So you have to be very careful when using it, and especially where is located phantom.exit.

As you're using console.log, I suppose that something spawns a phantomjs instance. Some techs like Node.js, internally uses line/memory buffers : as a consequence, if your process ends suddenly (unhandled exception), it is entirely possible that the buffered output will never reach the screen ; child process could also be killed if the amount of data allowed on stdout or stderr is exceeded (maxBuffer here)

At the end, If you want to write large contents, console.log may not be the best solution.

Cybermaxs
  • 24,378
  • 8
  • 83
  • 112