What's the difference between code inside Meteor.startup() and top-level code?

Sorry about a noob question. In this example:

  console.log("I am top level code");

  Meteor.startup(function () {
    console.log(" I am running inside startup");
  });

Both seem to run roughly at the same time (with startup running slightly later).

From the docs:

On a server, the function will run as soon as the server process is finished starting. On a client, the function will run as soon as the DOM is ready

On a client, startup callbacks from packages will be called first, followed by templates from your .html files, followed by your application code.

Is not it the same as top level code? When is top level called? On client and server.

One difference is that code inside Meteor.startup is called after all top level code has executed. This means if you want to set up some variables in top-level code, you can safely use them inside Meteor.startup inside another file (or package) and assume they are initialized.

3 Likes