NodeJS Promise how to distribute / slow down http.get calls

I am new to NodeJS and Promise functionality, so please be polite if this is an uninformed question.

I try to read the records database first and then check if the links really work (search for 200 answer). For my current test data, this should always return a 200 response. I get a 302 response (too many requests), and then the development server crashes. I need to slow down how I send queries to the database, but I cannot figure out how to do this. It seems to me that a promise simply sends everything as soon as it is resolved.

I tried to build time delays in this section, but to no avail. Here is the code:

var http404Promise = new Promise(function(resolve, reject) {
        var linkArray = new Array()

        db.sequelize.query(photoQuery, {
            replacements: queryParams
        }).spread(function(photoLinks) {
            photoLinks.forEach(function(obj) {
                var siteLink = hostname + 'photo/' + obj.img_id
                linkArray.push(siteLink)

                //console.log(siteLink);
            });

            resolve(linkArray);
        });
    });

http404Promise.then(function(linkArray) {
    linkArray.forEach(function(element) {
        console.log(element);
        http.get(element, function(res) {
            console.log(element);
            console.log("statusCode: ", res.statusCode); // <======= Here the status code
        }).on('error', function(e) {
            console.log(element);
            console.error(e);
        })
    })    
});
+4
source share
4

, - forEach , , forEach . , .
.

linkArray.forEach(function(element, index) {
    setTimeout(function() {
        console.log(element);
        http.get(element, function(res) {
            console.log(element);
            console.log("statusCode: ", res.statusCode); // <======= Here the status code
        }).on('error', function(e) {
            console.log(element);
            console.error(e);
        });
    }, index * 500);
});
+2
linkArray.forEach(function(element) {
        .....
    })

forEach Async eachLimit . , Async eachSeries, parallelism.

+1

async

https://github.com/caolan/async

,

linkArray = [];
var counter = 0;
http404Promise.then(function(responseData) {
  linkArray  = responseData;
  if (linkArray.length > 0) {
    requestHandler(linkArray.pop());
  }
});

function requestHandler(data) {
  http
    .get(data, function(res) {
      counter++;
      if (counter == linkArray.length) {
        console.log("finished");
      } else {
        reuqestCheckPromiss(linkArray.pop());
      }
    })
    .on("error", function(e) {
      counter++;
      if (counter == linkArray.length) {
        console.log("finished");
      } else {
        reuqestCheckPromiss(linkArray.pop());
      }
    });
}
+1
source

You can use the reduce function to create a pending request channel

    const timeout = 500;
    const linkArray = [1,2,3,4]
    
    const httpFunc = el => {
      // your http function here
      console.log('request num ', el)
    }
    
    
    const func = el => new Promise(resolve => {
      setTimeout(() => {
        resolve(httpFunc(el));
      }, timeout)
    })
    
    linkArray.reduce((acc, el) => acc.then(() => func(el)), Promise.resolve())
Run codeHide result
0
source

Source: https://habr.com/ru/post/1695645/


All Articles