title: Node/Express server starts before variable sets
body: <p>There is an issue on my node/express server startup in that it loads the server before the IP address function/script, which dynamically retrieves the hostIp from EC2 finishes. So my calls to a backend are all returning <code>undefined</code>.</p>
  
  <p>Example: (search.js)</p>
  
  <pre><code>const awsHost = require('./host.js');
  
  const hostIp = awsHost.getBackendHost();
  const host = `http://${ hostIp }:1234`;
  console.log(host);
  console.log(hostIp);
  console.log(awsHost.getBackendHost());
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>[nodemon] restarting due to changes...
  [nodemon] starting `node src/index.js`
  http://undefined:1234
  undefined
  Server: Success -port 1111
  11.123.456.789
  </code></pre>
  
  <p>Here is the current state of my (index.js):</p>
  
  <pre><code>const express = require('express');
  
  const app = express();
  
  app.use(express.static('./web/'));
  app.listen(1111, () =&gt; console.info('Server: Success -port 1111'));
  
  require('./routes/routes.js')(app);
  
  module.exports = app;
  </code></pre>
  
  <p>How can I force Node to run the <code>awsHost.getBackendHost()</code> function and load it before the final server start?</p>
  
  <p>EDIT (host.js):</p>
  
  <pre><code>const aws = require('aws-sdk');
  
  aws.config.update({ region: 'REGION' });
  
  const getBackendHost = () =&gt; {
  const ecs = new aws.ECS();
  const ec2 = new aws.EC2();
  
  const backendPort = '1234';
  const clusterParameters = {
    cluster: 'CLUSTER',
  };
  
  ecs.listContainerInstances(clusterParameters, (listContainerError, listContainerData) =&gt; {
    if (listContainerError) console.log(listContainerError, listContainerError.stack);
    else {
      const containerInstanceParameters = {
        cluster: clusterParameters.cluster,
        containerInstances: listContainerData.containerInstanceArns
      };
      ecs.describeContainerInstances(containerInstanceParameters, (describeContainerError, describeContainerData) =&gt; {
        if (describeContainerError) console.log(describeContainerError, describeContainerError.stack);
        else {
          describeContainerData.containerInstances.forEach((container) =&gt; {
            container.remainingResources.forEach((dataSet) =&gt; {
              if (dataSet.stringSetValue &amp;&amp; dataSet.stringSetValue.includes(backendPort)) {
                const containerInstanceId = {
                  InstanceIds: [
                    container.ec2InstanceId
                  ]
                };
                ec2.describeInstances(containerInstanceId, (describeInstanceError, describeInstanceData) =&gt; {
                  if (describeInstanceError) console.log(describeInstanceError, describeInstanceError.stack);
                  else {
                    const iP = describeInstanceData.Reservations[0].Instances[0].PrivateIpAddress;
                    console.log(iP);
                    return iP;
                  }
                  return describeInstanceError;
                });
              }
            });
          });
        }
      });
    }
   });
  };
  
  module.exports = {
    getBackendHost
  };
  </code></pre>