I observed the following when running vm.runInNewContext:
vm = require('vm');
fs = require('fs');
function f() {for (l=0;l<1000;++l) {var ll=l*l*l; console.log(1);}};
function g() { console.log(2)};
vm.runInNewContext( 'for (i=0;i<1000;++i) { fs.writeFile("/tmp/1_", "111", f); }' ,
{fs:fs, f:f, console:{log(){console.log.apply(console,arguments);}}},
{timeout:5000});
vm.runInNewContext( 'for (j=0;j<1000;++j) { fs.writeFile("/tmp/1_", "222", g); }' ,
{fs:fs, g:g, console:{log(){console.log.apply(console,arguments);}}},
{timeout:5000});
I would have expected the output to be interspersed with 1 and 2 but all the 1s are displayed first and then the 2s. In actual use, the code is sent asynchronously over the network to be executed in the vm and if one is in progress, it appears that the execution of the subsequent invocation will be delayed, even though the first code has async i/o calls. Is that correct? Any way to get around it. The code passed to vm will most likely use multiple promises with async i/o.