title: Using results of a callback in node in a separate function
body: <p>I'm probably missing the point somewhere here so I'm looking for advice.</p>
  
  <p>I have a nodejs server which is listening for client connections and, based on the data received, makes calls to an API.</p>
  
  <p>The very first call to that API gets an ID which needs to be used on subsequent calls to group them together.</p>
  
  <p>Where I'm struggling is that the call to the API is necessarily asynchronous and in the callback I'm assigning the ID to a variable. While that async call is being processed by the API server, more data is coming in from the client and needs more API calls made BUT I can't fire them until I know the results from the first call as the second calls depend on it.</p>
  
  <p>What's the proper way to handle this? I feel like I should be using Q to promise the results of the first API call to the second, but I'm not sure how it should be structured. Or should I just be queueing up the API calls until the first completes? How would I do that?</p>
  
  <p>Example problem code :</p>
  
  <pre><code>var server = net.createServer();
  
  //set up the callback handler 
  
  server.on('connection', handleConnection);
  
  handleConnection(conn) {
    //do some stuff...
    firstAPICall();
    conn.on('data', handleData);
  }
  
  handleData(data) {
    //do some stuff...
    otherAPIcall();
  }
  
  firstAPICall() {
    client.get("http://myAPI/getID", function (data, response) {
      conn.myID = data[0].myID;
      }
    }
  }
  
  otherAPICall() {
    //How do I make sure I actually have a value
    //in conn.myID from the first function???
    client.post("http://myAPI/storeData", { data: {myID:conn.myID, data:someData} }, function (data, response) {
      //do some stuff...
      }
    }
  }
  </code></pre>