-1

Am using a function to save data. Using the http.request option. It is working fine. If I call the same function in a loop, some of the data not saving in database. Also getting the parse error message for some response.

How Can I call the function of http.request in a loop?

for (var i = 1; i <= 23; i++) {
    turn_timer(i);
}

function turn_timer(nos) {
    try {
        var str = "num=" + nos;
        var len = str.length;

        win_settings.headers = {
            'Content-length': len,
            'Content-Type': 'application/x-www-form-urlencoded'
        }

        var request = http.request(win_settings, function(response) {
            response.on('data', function(data) {

            });

            response.on('error', function(err) {

            });

            response.on('end', function() {

            });
        });

        request.on('error', function(err) {

        });

        request.write(str + "\0");
        request.end();

    } catch (err) {
        console.log(err);
    }
}
3
  • can you share your code? Commented Nov 19, 2014 at 4:29
  • Have you read this stackoverflow.com/questions/21320159/… ? Commented Nov 19, 2014 at 4:30
  • added the code. it accepts the first five correctly and then next 5 error. next few ok and again error. is it due to socket hanging while waiting for response ? how to fix it? Commented Nov 19, 2014 at 4:36

2 Answers 2

0

Check the scope of your variable that stores message because async function call may overwrite it.

Sign up to request clarification or add additional context in comments.

Comments

0

I believe your problem is because you are using a for loop, instead of going with a more asynchronous approach. Here is a quick attempt to solve your problem. I have omitted some of your code, as it seemed to be incomplete. I have left the important parts and added a few things based on an answer to a similar question.

var http = require('http'),
    async = require('async');

function turn_timer(n, callback) {
    var var str = "num=" + n,
        len = str.length,
        req;

    win_settings.headers = {
        'Content-length': len,
        'Content-Type': 'application/x-www-form-urlencoded'
    };

    req = http.request(options, function(response) {
        ...
        callback(null);
    });

    req.on('error', function(err) {
        ...
        callback(err);
    });

    req.end();
}

async.timesSeries(23, function (n, next) {
    turn_timer(options, function(err) {
        next(err);
    });
});

For more information, you can read more about async.timesSeries here: https://github.com/caolan/async#timesseriesn-callback

2 Comments

how this will execute the same function with different values ? like a loop am expecting
It will receive the number as the first argument. You can use that to calculate any values that you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.