1

I have such function:

function getStatusCode(site){
    var options = {
        host: "127.0.0.1",
        port: 8000,
        path: site,
        headers: {
                    Host: site
            }
        };

    var status;

    http.get(options, function(response) {
        status=response.statusCode;
    });
    return status;
} 

How to return status only when http.get finish work or return null if timeout(e.g. 10 seconds)?

Anton Kandybo
  • 3,678
  • 4
  • 23
  • 31
  • I would rethink what you are trying to do. It looks like you want a synchronous `getStatusCode` function, but that isn't the way things are usually done. – Ray Toal Nov 21 '11 at 08:12

3 Answers3

10

The whole idea behind node.js is its asynchronous nature. What you are trying to do is to violate the design pattern that is at the very root of it by trying to make it synchronous.

Due to the asynchronous nature of the get method you cannot do this. You can manipulate the status code only inside the callback function. So your getStatusCode doesn't need to return anything. You could make it take an additional parameter which will represent a callback invoked once the results are available:

function getStatusCode(site, callback) {
    var options = {
        host: "127.0.0.1",
        port: 8000,
        path: site,
        headers: {
            Host: site
        }
    };

    http.get(options, function(response) {
        callback(response.statusCode);
    });
}

and then:

getStatusCode('http://...', function(statusCode) {
    alert(statusCode);
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    You forgot to add error handling.. `http.get(options, function(res) { // callback }).on('error', function(e) { // Error handling });` – pradeek Nov 21 '11 at 08:19
2

The proposed approaches don't scale well, especially if you need error handling: you will get spaghetti of lambdas in complex cases. So the examples above should be treated like an entry-level tutorial and shouldn't be used in production.

You can use return to return values asynchronously, but you should return not a value but some kind of continuation. Futures and promises are one example of such continuations. Look for NPM libraries providing them. See q and q-http libraries for an example.

See also the following related questions:

What is the benefit of a 'promise' abstraction in CommonJS?

Understanding promises in node.js

I think q library is distinguished because it has sister libraries (q-io, q-http) implementing most of the standard node.js library using promises.

In fact, an early standard library for node used promises, but later they decided to use callbacks. It was not because of promises was not good, but because callbacks are lower-level.

Community
  • 1
  • 1
nponeccop
  • 13,527
  • 1
  • 44
  • 106
1

You can't "return" it per se, since that would require the function block on i/o and that is a node no no. Instead, you can pass it a callback function, that will be called with your status code.

function getStatusCode(site, callback){
    var options = {
        host: "127.0.0.1",
        port: 8000,
        path: site,
        headers: {
            Host: site
            }
        };

    var status;

    http.get(options, function(response) {
        status=response.statusCode;
        callback(status);
    });
} 
rob
  • 9,933
  • 7
  • 42
  • 73