When you require a module, or when you must load any file in to memory, before 
your program can actually accept any input, it's better to use sync file IO. In 
other words, if there is nothing your program can do until this file IO 
operation is complete then there is no benefit in not blocking the vm until it 
returns. In this case it's actually faster to do a synchronous file operation 
and halt the execution until the operation is complete.

After the "setup phase" (the time from when your program starts until the first 
nextTick()) you should not use synchronous IO because there are other 
concurrent IO events to process while you do file IO so it's better not to 
block any execution.

-Mikeal

On May 15, 2012, at May 15, 201212:50 PM, Jorge wrote:

> Async is always best, but sometimes not :-P
> 
> On May 15, 2012, at 8:30 PM, Martín Ciparelli wrote:
> 
>> So the question would be: why an async-driven platform uses sync-driven way 
>> to load modules?
>> 
>> 2012/5/15 Marak Squires <marak.squi...@gmail.com>
>> require() is a sync call.
>> 
>> If you need an async require ( a require that performs i/o ), you should 
>> check out a plugin system like Broadway ( 
>> https://github.com/flatiron/broadway )
>> 
>> This will allow you to require modules with an optional .init and .attach 
>> methods, which can both accept optional callbacks.
>> 
>> 
>> On Tue, May 15, 2012 at 8:49 AM, Guoliang Cao <gca...@gmail.com> wrote:
>> Hi,
>> 
>> I'm new to Node.js and wrapping my head around the asynchronous programming 
>> model. A question comes to mind is why 'require' itself does not use 
>> callbacks like below. I believe IO is involved in require and a lot of 
>> things can fail.
>> 
>> require('http', 
>> function(http){
>>   // do stuff with http
>> },
>> function(error) {
>>   console.log("Failed to load http");
>> });
>> 
>> 
>> Thanks,
>> Guoliang Cao
>> 
>> -- 
>> 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 nodejs@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>> 
>> 
>> 
>> -- 
>> -- 
>> Marak Squires
>> Co-founder and Chief Evangelist
>> Nodejitsu, Inc.
>> marak.squi...@gmail.com
>> 
>> 
>> -- 
>> 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 nodejs@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>> 
>> 
>> -- 
>> 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 nodejs@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
> 
> -- 
> 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 nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

-- 
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to