简体   繁体   中英

Breaking an infinite loop in Node.js/Express.Js

I've created an Express app that is supposed to be paginating through an external API call. I've looked at this every which way and I can't figure out why the function isn't tripping the condition to break the loop. Any help would be appreciated!

Looks like I can't break from the last loop, calling the makeRequest function. Evidence for the infinite loop is the first console.log in the request callback, "I'm making a request." I had more console.logs further down in the callback function that should also always return something but it doesn't even seem to get to those.

app.post("/api/example", function(req, res) {
    var username = username;
    var password = password;
    var container = [];
    var counter = 0;
    var keepGoing = true;

    var makeRequest = function() {
        console.log("I'm making a request");
        var URL = "https://fakeapiurl.com/&page=" + counter; 
        request.get(URL, { 
            'auth': {
                'user': username, 
                'pass': password,
                'sendImmediately': true
            },
            'content-type': 'application/json'
            }, function(error, response, data) {
                var results = JSON.parse(data);
                var examples = results.examples;
                var numOfExamples = results.numResults;
                console.log(numOfExamples);

                if ((numOfExamples === 0) || (numOfExamples === jobsContainer.length - 1)) {
                    counter = 5;
                    keepGoing = false;
                } else {
                    counter++;
                    for (var i = 0; i < examples.length; i++) {
                    container.push(examples[i]);
                    }
                } 

                if (counter === 5) { 
                    keepGoing = false;
                    container.sort(function(a, b) {
                        etc.
                    });

                    res.send(container); 
                }
            });// end of request call 
        };// end of makeRequest function

    while (keepGoing === true) {
        makeRequest();
    }

});// end of app post

This will never work like you would expect, you're firing async requests inside a sync while loop. So at the time the first request is trying to get the data, you're firing the same request again, so your first request gets canceled. This goes like forever. You should fire the next request inside the success callback of the previous request, so it gets fired after the previous one resolves.

Something like that:

app.post("/api/example", function(req, res) {
var username = username;
var password = password;
var container = [];
var maxPages = 5;
var makeRequest = function(page) {
    console.log("I'm making a request");
    var URL = "https://fakeapiurl.com/&page=" + page; 
    request.get(URL, { 
        'auth': {
            'user': username, 
            'pass': password,
            'sendImmediately': true
        },
        'content-type': 'application/json'
        }, function(error, response, data) {
            var results = JSON.parse(data);
            var examples = results.examples || [];
            var numOfExamples = results.numResults;
            var lastPageReached = (numOfExamples === 0 || numOfExamples === jobsContainer.length - 1);
            lastPageReached = lastPageReached  && page < maxPages;

            if (lastPageReached) {
                container.sort(function(a, b) {
                    etc.
                });
                res.send(container); 
            } else {
                container = container.concat(...examples);
                makeRequest(page + 1);
            } 
        });
    };
  makeRequest(0);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM