HTTP request callback not executing - why?
<p>I'm attempting to submit an HTTP POST request to a 3rd-party API. It's currently wrapped in a promise method. </p>

<p>The callback function is never executed, however when I run the code below just by itself, the call to the API is successful. </p>

<p>I'm fairly new to Node.js and the idea of an event-loop. What exactly is happening behind the scenes and why does the callback function not execute?</p>

<p><strong>Code:</strong></p>

<pre><code>// SQL Query successfully called and passed into done() function
.done(function(sqlResult) {

// contact already exists. resolve the issue
if(sqlResult[0]) {
    deferred.resolve("Contact already exists in database.");
}

// no contact found. create new HubSpot contact
else {
    console.log("No contact found. Creating HubSpot contact...");

    // create the POST data object
    var postData = querystring.stringify({
        'email': contact.email,
        'firstname': contact.first_name,
        'lastname': contact.last_name,
        'hs_context': JSON.stringify({
            'hutk': contact.cookie,
            'ipAddress': hs_context.ipAddress,
            'pageUrl': hs_context.pageUrl,
            'pageName': hs_context.pageName
        })
    }),

    // set POST options
    options = {
        hostname: 'forms.hubspot.com',
        path: '/uploads/form/v2/xxxxxx/' + formID,
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': postData.length
        }
    };

    // set up request
    console.log("Setting up HubSpot API request.");
    var request = http.request(options, function(response) {
        // THIS CALLBACK DOES NOT EXECUTE
        console.log("TEST - INSIDE REQUEST CALLBACK.");
        console.log("Status: " + response.statusCode);
        console.log("Headers: " + JSON.stringify(response.headers));
        response.setEncoding('utf8');
        response.on('data', function(chunk) {
            console.log("Body: " + chunk);
        });
    });

    // handle any errors
    request.on('error', function(e) {
        console.log("Problem with request - " + e);
    });

    // Post the data
    console.log("Posting data.");
    request.write(postData);
    request.end();
} // end of else 

...
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>No contact found. Creating HubSpot contact...
Setting up HubSpot API request.
Posting data.
</code></pre>

<p>Any suggestions? I received the code from Hubspot's API docs - <a href="http://developers.hubspot.com/docs/methods/forms/submit_form" rel="nofollow">http://developers.hubspot.com/docs/methods/forms/submit_form</a> - and tweaked to fit my needs.</p>

<p><strong>Update:</strong></p>

<p>I've noticed several of my tests have succeeded in making the API call. None of the <code>console.log()</code> are displaying, but when I go to the 3rd-party website I see every now and then the API call is successfully made.</p>

<p>I've made a quick time stamp at when I ran my tests.</p>

<pre><code>12:09:12 - Unsuccessful
12:10:45 - Unsuccessful
12:11:05 - Unsuccessful
12:11:14 - Unsuccessful
12:11:32 - Successful!
</code></pre>

<p>I'm still struggling to understand how this can happen ...</p>