Return Promise From REST Call
<p>I'm trying to invoke a REST call and return a promise so I can manipulate the data afterwhich.</p>

<pre><code>var self = this;
var returnPromise;
returnPromise = self.httpService.testService();
returnPromise.then(function(result){
 console.log(result);
});
</code></pre>

<p>My REST service is located <strong>in another file</strong> which has the following:</p>

<pre><code> testService() {
    console.log("start of testService");

    var request = require('request');
    var user = "test";
    var ipadd = "127.0.0.1";

    request({

        url: 'https://' + 'LocalHost' + '/URLOFWEBSERVICE',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        form: {
            userID: user,
            userIpAddress: ipadd
        }
    }, function(error, response, body) {
        if (error) {
            console.log(error);
        } else {
            console.log(response.statusCode, body);
            var newPro = new Promise(function(fulfill, reject) {
                fulfill(body);
            });

            console.log(newPro);
            return newPro;

        }
    });
}
</code></pre>

<p>I'm able to print out response.stateCode , body( REST result) and the "invoked fulfill" console is printed out.</p>

<p>The problem lies with the </p>

<blockquote>
  <p>returnPromise.then</p>
</blockquote>

<p>in the <strong>first page</strong>, in which <strong>".then"</strong> returns me undefined.</p>

<p>At first I thought that it might be the promise has been called before the REST returns me a response. So, I thought of doing a timeout function to test.</p>

<pre><code>setTimeout(
function(){
  console.log(returnPromise);
}
, 5000);
</code></pre>

<p>But even so, returnPromise returns me "undefined".</p>