I have a nested for loops Nodejs code, when I print out the result, the order is messed up, and I also got the UnhandledPromiseRejectionWarning
due to async
#!/usr/bin/env node
'use strict';
let Parser = require('rss-parser');
let parserrss = new Parser();
const regions = ['us-east-1', 'us-west-2']
const services = ['s3', 'elasticache', 'management-console', 'cloudwatch', 'ec2', 'elb', 'rds', 'route53', 'vpc', 'iam',]
for(let i=0; i < regions.length; i++){
for(let j=0; j<services.length; j++){
let region = regions[i]
let service = services[j]
if (region == '' || service =='management-console' || service == 'route53'){
var feed = service
}
else{
var feed = service + '-' + region
}
const url = `http://status.aws.amazon.com/rss/${feed}.rss`
//console.log(url)
parserrss.parseURL(url).then(d => {
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
const date = new Date()
const month = monthNames[date.getMonth()]
if(date.getDate().toString().length==1){
var day = "0" + date.getDate().toString()
} else {
var day = date.getDate().toString()
}
const year = date.getFullYear().toString()
if(d.items && d.items[0]["pubDate"].substring(5, 16)==`${day} ${month} ${year}`){
console.log(`${region}-${services} event title: ${d.items[0]["title"]}`)
console.log(`${region}-${service} event date: ${d.items[0]["pubDate"]}`)
console.log(`${region}-${service} event description: ${d.items[0]["contentSnippet"]}`)
}
else if (d.title){
console.log(`${region}-${service} status OK: No events to display`)
}
})
}
}
This is the output, the order is not right, also there are 2 records are not what I am expected, which are indicated by the red mark
How can I fix it to make it print the correct order and records? I know I have to use aysnc/await
function. actually I implemented it with async and callbacks. However, I am get the same issue. I am new to Nodejs
and async
world, no idea how to implement it in my code.