• Announcements

    • MannDude

      Current state of vpsBoard   02/04/2017

      Dear vpsBoard members and guests:

      Over the last year or two vpsBoard activity and traffic has dwindled. I have had a change of career and interests, and as such am no longer an active member of the web hosting industry.

      Due to time constraints and new interests I no longer wish to continue to maintain vpsBoard. The web site will remain only as an archive to preserve and showcase some of the great material, guides, and industry news that has been generated by members, some of which I remain in contact to this very day and now regard as personal friends.

      I want to thank all of our members who helped make vpsBoard the fastest growing industry forum. In it's prime it was an active and ripe source of activity, news, guides and just general off-topic banter and fun.

      I wish all members and guests the very best, whether it be with your business or your personal projects.

      -MannDude
Sign in to follow this  
Followers 0
Munzy

NodeJS Async function help!

2 posts in this topic

Alright, so I am new to nodejs from php. As most know, php being linear/blocking made coding *easy* per-say. However, I have found myself loving nodejs, and I am having a hard time wrapping my mind around how to let a nodejs function run async, but then wait for responses of that async function before continuing.

 

Here is some example code

var ping = require("ping");

var verHosts = [
'192.168.230.118',
'192.168.230.25'
];

var verStatus = 0;

verHosts.forEach(function(host) {
        ping.sys.probe(host, function(isAlive){
            if(isAlive == true) {
                verStatus++;
            }
        })
    });
    console.log(Date() + " " + "Verified status is at: " + verStatus);
    if(verStatus > 0) { // continue shiz and giggles here after I get the above};

 

If you could elaborate as I am trying to learn as well, not just copy paste in dummy mode!

 

Thanks!

Share this post


Link to post
Share on other sites

Basically, you'd use Promises. Any kind of looping or conditional asynchronous work is very hard to get right with callbacks alone.

I've put together a reading list about Promises here, that should get you started - Promise.map is most likely what you're looking for in particular :)

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0