Hi I am new to NodeJs and Javascript. I have a requirement to return a Map of key value from a promise type call. I am calling NodeJs ping module where I want to make asynchronous call to ping the IP addresses and I want to store IP address as key and reachable status as value in a Map. My code is working where I am able to print in the console but I am not able to return a Map. I provide below the code. Please help me as I am struck now.
import { FileUtil } from "./util/file-util";
import ping = require("ping");
export class Test1 {
public async pingManyIPAddress(): Promise<Map<string, boolean>> {
let map: Map<string, boolean> = new Map();
let fileUtil = new FileUtil();
const fileNamePath: string = "testData/ipaddress-50.txt";
const allIPs: string[] = await fileUtil.readFileLineByLineNonBlocking(fileNamePath);
let cfg = {
timeout: 10 // Timeout in seconds
};
allIPs.forEach(ip => {
ping.promise.probe(ip, cfg).then((data: any) => {
let ipAdrs = ip;
let status = data.alive;
console.log(ip,":",status); //Prints all IP and reachable status
map.set(ip, status);
});
});
// How to wait till the above promise line gets completely executed
return Promise.resolve(map);
}
public async justCheck(): Promise<void> {
let map: Map<string, boolean> = await this.pingManyIPAddress();
for (let key of Object.keys(map)) {
console.log(key + " <-> " + map.get(key));//It does not print
}
}
}
let test2 = new Test1();
test2.justCheck();
Please guide me to learn so that I can modify the above code to return a Map.