onload?

1,083 views
Skip to first unread message

Michael DeMond

unread,
May 18, 2014, 5:20:58 PM5/18/14
to nod...@googlegroups.com
Hello Community,

Total Node newb here. :)  I just started a few weeks ago and have been learning it.  I am curious if there is an equivalent to a window.onload event handler in NodeJS.  Essentially, I am looking to execute a delegate when all of the code in my NodeJS application has loaded.

Thank you for any assistance you can lend,
Michael

Bence Dányi

unread,
May 18, 2014, 5:39:37 PM5/18/14
to nod...@googlegroups.com
Hi Michael,

in node, `require` is synchronous, so you don't have to wait for any event.

Bence
:wq
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+un...@googlegroups.com.
> To post to this group, send email to nod...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/d3f86cea-2079-4c89-9a46-bba931f2850a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Francesco Mari

unread,
May 18, 2014, 5:31:57 PM5/18/14
to nod...@googlegroups.com

You are responsible to load your code by requiring the different modules you application is composed of.

Depending on the architecture and the complexity of your application, you can organize your code in subsystems. Even in this case, though, you are responsible to define when a subsystem is considered to be fully loaded and initialized.

--

// ravi

unread,
May 18, 2014, 8:09:42 PM5/18/14
to nod...@googlegroups.com
On May 18, 2014, at 5:20 PM, Michael DeMond <michae...@live.com> wrote:

Total Node newb here. :)  I just started a few weeks ago and have been learning it.  I am curious if there is an equivalent to a window.onload event handler in NodeJS.  Essentially, I am looking to execute a delegate when all of the code in my NodeJS application has loaded.


There is, as you may be aware, a difference between your code loading and your code being ready. Since require() is synchronous, if you require() all your modules at the outset your code has, speaking roughly, loaded. But is it ready? Your modules may be doing asynchronous things during initialisation e.g, connecting to a DB. Can you start accepting clients and processing requests once your require()s are done? Depends on how you structure your code (*mumble*promise*mumble*).

I hope I am not leading you down a side path,

—ravi


Aria Stewart

unread,
May 18, 2014, 7:53:13 PM5/18/14
to nod...@googlegroups.com

On May 18, 2014, at 5:31 PM, Francesco Mari <mari.fr...@gmail.com> wrote:

> You are responsible to load your code by requiring the different modules you application is composed of.
>
> Depending on the architecture and the complexity of your application, you can organize your code in subsystems. Even in this case, though, you are responsible to define when a subsystem is considered to be fully loaded and initialized.

And generally, code in node loads synchronously, so you just start it at the end. It’s a strange design that has to resort to asynchronous loading, and therefore won’t know when it’s done without work.

signature.asc

Ryan Schmidt

unread,
May 18, 2014, 7:54:08 PM5/18/14
to nod...@googlegroups.com

On May 18, 2014, at 16:20, Michael DeMond wrote:

> Total Node newb here. :) I just started a few weeks ago and have been learning it.

Welcome!

> I am curious if there is an equivalent to a window.onload event handler in NodeJS. Essentially, I am looking to execute a delegate when all of the code in my NodeJS application has loaded.

window.onload is an invention of web browsers, which gets called when the DOM has been processed and all external resources including scripts, stylesheets and images have been loaded. Node is not a browser so it doesn't have most of those concepts so there is no equivalent to window.onload in node.

Code that does things asynchronously (which is the norm in node) should be calling a callback when it's finished. So if you want to do something when that asynchronous code is done, you do it in the callback.

It can get a bit complicated when you have multiple things you need to do and wait for them all to complete. There are control flow libraries that can help you manage the complexity; async is a popular one, but there are others. You don't need to use a library, of course; you could do something as simple as keep a counter of how many tasks you've started, then decrement the counter after each task completes; when the counter reaches 0, all tasks have been completed and you can do whatever you wanted to do next.



Kevin Ingwersen

unread,
May 19, 2014, 2:52:34 AM5/19/14
to nod...@googlegroups.com
Hey.

No, there is no such event in nodejs. But there are libraries that offer such functionality, as far as I am aware. But, I would suggest something like this.

var myModule = require(…);
function initialize() {
// put some code here
}
(function(){
initialize();
// do stuff here.
})();

I saw this sort of structure being used in the web as well as in nodejs. From what I know about javascript itself: the function expression is evualted, and then executed immediately. Since its after your init function, it should be ran just then, when your code is set up.

But dont take my word for the gold; I could be all wrong too. :) But its a point to start at, I bet.

Kind regards, Ingwie.


--
"Yo Dawg, I heard you like C++, so I  added C++ to your build system, so you have to compile before you compile.“ -StackOverflow, Matt Joiner ( http://stackoverflow.com/a/5025525/2423150 )

Michael DeMond

unread,
May 19, 2014, 8:36:25 AM5/19/14
to nod...@googlegroups.com, ingwi...@googlemail.com
WOW.... thank you so much everyone for your help, comments and feedback.  Now I know for a fact where to go whenever I have a question about Node.js. :)

My problem had to do with how I was packaging my code for my Node.js application.  Essentially, I was creating a single "application.js" file, but the contents would be:

<entry.js>
<dependency.js>

entry.js was referencing elements in dependency.js, but since dependency.js was being emitted *after* entry.js, I was getting a ReferenceError at runtime.  By putting my entry.js in a setTimeout (with 0ms), I was able to work around this problem.  setTimeout has always seemed like a hack to me, so I try to avoid it at all costs, but here it seems like a good use of it.

Anyways, I was able to address my problem.  Thanks again all for your help!
Michael

Angel Java Lopez

unread,
May 19, 2014, 11:50:52 AM5/19/14
to nod...@googlegroups.com
I don't know why you need a "single file" for your application.

Usually, your main app do:

// app.js
var entry = require('./entry.js');

If entry.js needs dependency, then

// entry.js
var dependency = require('./dependency.js');

If you need dependency from app.js too, then

// app.js
var dependency = require('./dependency.js');

dependency.js is loaded only one, the second require returns a "cache" version.

I usually make a single file, not for Node.js, but for JavaScript modules (developed for node) that now I want to run on browser.

What is your use case?

Angel "Java" Lopez
@ajlopez



// ravi

unread,
May 19, 2014, 11:51:57 AM5/19/14
to nod...@googlegroups.com
On May 19, 2014, at 8:36 AM, Michael DeMond <michae...@live.com> wrote:
My problem had to do with how I was packaging my code for my Node.js application.  Essentially, I was creating a single "application.js" file, but the contents would be:

<entry.js>
<dependency.js>

entry.js was referencing elements in dependency.js, but since dependency.js was being emitted *after* entry.js, I was getting a ReferenceError at runtime.  By putting my entry.js in a setTimeout (with 0ms), I was able to work around this problem.  setTimeout has always seemed like a hack to me, so I try to avoid it at all costs, but here it seems like a good use of it.


Michael, if entry.js has a dependency on dependency.js, then I’d suggest require()’ing dependency.js in entry.js (require caching should, in most cases, avoid reloading dependency.js even if it is multiply required). Or load dependency.js first. I think you may be using “emitted” wrongly, above, unless there is actually some event being emitted by dependency.js which is listened to in entry.js.

I think you are right in your wariness of using a setTimeout hack. I’d suggest not using it here as well.

—ravi

Michael DeMond

unread,
May 19, 2014, 9:08:17 PM5/19/14
to nod...@googlegroups.com
Thanks again all for your replies.  I guess it would help to shed some light that I am also learning TypeScript and NodeJS for Visual Studio 2013 in the middle of all of this, and one of the settings there is to compile all javascript within a TypeScript application into a single file.  There are a lot of pieces to fit together here, so I am just exploring all possibilities.  I am also checking out CommonJS vs. RequireJS to really figure out the best way of structuring my application.  As noted, perhaps combining everything into one file is not the best approach, and leveraging module-loading is the better way to go.

Thanks again,
Michael


You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/DFzVn3YBbM0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.

To post to this group, send email to nod...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages