I am new to NodeJS and to Promise functionality so please be polite if this is an ignorant question.
I'm trying first read a database of records and then check the links actually work (looking for a 200 response). For my current test data this should will always return a 200 response. I'm getting 302 (too many requests) response and then the development server crashes. I need to slow down how I send through requests to the database but I cannot work out how to do it. It seems to me the promise simply sends everything as soon as it is resolved.
I have tried building in time delays in the then section, but to no avail. Here's 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's the status code
}).on('error', function(e) {
console.log(element);
console.error(e);
})
})
});