I have an array of 100 sites and I need to see if they are available or not. If they are available I need to check the response body for certain content, I also need to check the MX records.
I can do this pretty easily synchronously but it’s takes a long time.
I can’t work out how I would approach this with promises. I’m sure I’m missing something. Can someone point me in the right direction.
Thanks for your help.
All the tutorials I look at run for 1 url not 100s
2
Answers
You can do this in Node by executing async functions within an async loop. Any HTTP requests made in the async function won’t wait for responses before proceeding.
Of course, you’ll still need to solve for all the CORS access and such.
Here’s a simplified example:
If its NodeJS and not Javascript, you could use Axios and push the results to an array, like this:
Or, If you don’t want a promise-based approach, you could remove the asynchronous function calls:
Though be aware: using the non-promised based approach may occasionally not work, because you are not waiting for a promise change from the
axios.get
functionFeel free to let me know if something doesn’t work and I will be glad to check!