Reputation: 529
Looking for this example (and with my previous knowledge's in event-driven). I can say that this two routines run independently and in parallel (so will not block the hole program).
//....declaration and methods
register_1.on("execut_routine _1", function() { console.log("executed the routine 1"):})
register_2.on("execut_routine _2", function() { console.log("executed the routine 2"):})
But if i make this change:
//....declaration and methods
register_1.on("execut_routine _1", function() { /*Long Blocking Operation*/})
register_2.on("execut_routine _2", function() { /*Long Blocking Operation*/})
The first triggered event will block the happening of the second, right? If so, How can I reverse that effect?
Upvotes: 1
Views: 49
Reputation: 19842
Javascript is [generally] not a multi-threaded environment and so even in your first example, the reality is that they execute serially and not in parallel. Thinks like XHR's run in parallel, but they run as a function of the browser and not Javascript directly (i.e. the network requests run in parallel), but the javascript handlers will always fire serially.
Upvotes: 0
Reputation: 8666
Javascript is a single-threaded environment, so unless you are doing input/output related tasks (waiting on the file system, calling an external resource, etc), your code must wait for other code to finish.
Using event-driven programming styles will not help you with this problem.
However, you can tell your code to go into a sort of "sleep" mode, where it waits for other code to execute, and begins again at the next tick of the event loop. Node.js, for example, uses process.nextTick(fn)
to register code that should execute over multiple ticks.
Upvotes: 3