On Sat, Mar 17, 2012 at 14:04, Bjorn <[email protected]> wrote:
> However, can you please clarify one comment you had?
> "However, it's not clear from reading
> this code whether the function passed to websocket.onMessage will be
> called immediately or at some time in the futureĀ "
>
> I assumed the function I pass will only be called once onMessage is called,
> ie the example code is like an echoserver that doesn't care about it's input
> but only responds with the accumulated buffer. Sure, that's a wierd example
> but the point was to illustrate the potential race condition. =)
> BUT still, I'm pretty sure I'm missing some other finer aspects of
> Javascript - how is it unclear if that function is called immediately or at
> some point in the future?
Your example was something like this:
```
blah.onMessage(function () { ... })
```
It's not clear what onMessage is doing with the function you provide
it. It looks a bit like a node.js EventEmitter.
The EventEmitter class is a relatively simple JavaScript thing
(though somewhat overly optimized to the point of obfuscation if you
look at the code). The basic semantics are:
on('ev', fn) -> Add fn to list named 'ev'
emit('ev') -> Call all the functions in the list named 'ev'
There's no async stuff to it at all, though you'll typically call
'emit' in a different tick than you call 'on', so these objects make
async programming a lot easier.
Consider these two examples:
```javascript
// example A
var ee = new EventEmitter;
ee.on('foo', function () {
console.log('foo')
})
ee.emit('foo')
console.log('bar')
```
```javascript
// example B
var ee = new EventEmitter;
ee.on('foo', function () {
console.log('foo')
})
setTimeout(function () {
ee.emit('foo')
}, 100)
console.log('bar')
```
In A, we'll see "foo bar" logged, because we're emitting the 'foo'
event right away, inline. In B, we'll see "bar foo" logged, because
we're emitting 'foo' 100ms later.
So, when you do:
```
x.onSomething(fn)
```
it's not clear (without seeing the code) if the x object is going to
call the function right away, or attach it to some list to be called
at some point in the future. So, will it happen before the
setInterval, or after? It's not clear.
What IS clear is that the setInterval callback will not fire *in
parallel* with the onMessage event handler.
When you get the execution context, no one can take it away from you.
If you do `while(1);`, then you'll never go back to the event loop,
timeouts won't time out, requests won't be made, responses won't be
handled, etc. So don't do that :) But, the order of who's getting
control of the execution context in which order, in real life where
timing is unpredictable, can be arbitrary. It's often a good practice
to assume that your functions will be called out of order, or all at
the same time, etc.
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en