On Sun, Feb 12, 2012 at 8:17 PM, Ben Noordhuis <[email protected]> wrote:
> On Mon, Feb 13, 2012 at 00:22, Mark Volkmann <[email protected]> > wrote: > > Here's a snippet from the example code in the docs for the TLS module: > > > > var cleartextStream = tls.connect(8000, options, function() { > > console.log('client connected', > > cleartextStream.authorized ? 'authorized' : > 'unauthorized'); > > process.stdin.pipe(cleartextStream); > > process.stdin.resume(); > > }); > > > > > > Note how the callback function passed to tls.connect uses > cleartextStream. > > That is returned from tls.connect. > > This seems unusual. Is it safe to assume that the callback will be > invoked > > after the function returns? > > Yes. It's an assumption that's true for everything that involves I/O. > The callback runs at the next tick of the event loop (or later) but > never at the current tick. > I didn't know about that assumption. Thanks for explaining it! > > I wonder why cleartextStream isn't just passed to the callback rather > than > > being returned from tls.connect. > > Because this === cleartextStream in your callback. The callback is > registered as a listener for cleartextStream's 'secureConnect' event > and listeners always run in the context of the emitting object. That seems like something that deserves to be mentioned in the docs. Given that, I think the client code example should replace all references to "cleartextStream" with "this". -- R. Mark Volkmann Object Computing, Inc. -- 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
