Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - Pausing script execution in another thread

.:Disclaimer:.

Please note that I am trying to achieve something with node.js which might go against its design principles. However as node.js goes against the norm of "javascript is for client side", I am also trying something different here. Please bear with me.

.:Background:.

I have a requirement where Java Scripts need to be narrative (read from beginning to end) for simplistic scripts for simplistic users. I will also offer an Async scripting ability for more advanced users.

I understand the event driven methodology of node.js, and have a solution working based on Async callbacks. Now I am trying to simplify this for our more basic scripting requirements.

.:The Question:.

Is there a way to run a script (using its own sandbox) where execution can be paused while a result is being delivered.

E.g.

var util = require('util'),
vm = require('vm'),
user = require('user');

vm.runInThisContext('user.WaitForProceed(); console.log('Waiting over');');

console.log('Finished!');

User is my own module that will do the waiting. Basically I want it to sit there and block on that line in the vm object until it has received a result back. After which it will continue onto the console.log line.

The output of this example unimportant as it is also achievable through callbacks. The narrative nature of the example script is more important for this solution.

  • J
like image 760
Nevar Avatar asked Dec 02 '25 05:12

Nevar


1 Answers

There is no way to pause the execution in Node. At least they tell us so :)

There are some libraries which support an advanced flow control, like Invoke, maybe this could help, but I understands, that's not what you asked for :)

Also you could implement a busy-loop using nextTick()

Or you could implement a blocking call in C(++) and provide it as a library. I never did this.

One last way is to readFileSync() to a namedpipe which closes on a certian event.

As you already mentioned, it's against the language principes, therefor these solutions are all hacky.

like image 170
Gottox Avatar answered Dec 03 '25 19:12

Gottox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!