title: Node.js execution order
body: <p>I am quite new to Node.js and I am trying to load json configs files stored either in Amazon s3 or local repository. Below is my code so far:</p>
  
  <pre><code>var cfg = process.env.CONFIG_FILE_NAME;
  
  log.info("Loading config '%s'", cfg);
  
  if(cfg.indexOf("s3") !== -1 || cfg.indexOf("S3") !== -1) {
      log.info("S3 path detected");
      var s3 = new aws.S3();
      var myRegex = /\/\/(\w*)\/(.*)/g;
      var matched = myRegex.exec(cfg);
      var bucket = matched[1];
      log.info("Extracted bucket: ", bucket);
      var key = matched[2];
      log.info("Extracted key: ", key);
  
      var params = {
        Bucket: bucket, 
        Key: key
      };
      s3.getObject(params, function(err, data) {
        if (err) log.warn(err, err.stack);
        else {
            log.info("Loaded config from S3");
            cfg = JSON.parse(data.Body);
            log.info("Config content: "cfg);
        }
      });
  
  }
  else {
       try {       
            //some code here      
          } catch (e) {
           //some code here
          }
  
  }
  
  
  subscriptions = cfg.subscriptions;
  log.info("This supposes to contain json content from S3: ", cfg);
  </code></pre>
  
  <p>The idea is that the code will check if there is a path to S3 in the message sent to Amazon Lambda (CONFIG_FILE_NAME field). If it exists, then the code load the config file from s3, otherwise, it loads locally. However, when I try to run the code, it returns something like this:</p>
  
  <blockquote>
    <p>4 Jan 11:37:34 - [INFO] Loading config 'Path-to-S3'</p>
    
    <p>4 Jan 11:37:34 - [INFO] S3 path detected</p>
    
    <p>4 Jan 11:37:34 - [INFO] Extracted bucket:  mybucket</p>
    
    <p>4 Jan 11:37:34 - [INFO] Extracted key:  mykey.cfg.json</p>
    
    <p>4 Jan 11:37:34 - [INFO] "This suppose to contain json content from S3: Path-to-S3'</p>
    
    <p>4 Jan 11:37:34 - [INFO] Loaded config from S3</p>
    
    <p>4 Jan 11:37:34 - [INFO] Config content: my-config-content</p>
  </blockquote>
  
  <p>So the problem is that, the code executes the line <code>subscriptions = cfg.subscriptions;</code> before the config file is loaded from S3. The variable cfg at this line only contains the path to the config, not the config content I want to load from S3. My later code implementation depends on this subscriptions field from cfg file so it stucks right here.</p>