2

please help!

How to breaking if available true?

I use node js tcp-ping module.

My code

var tcpp = require('tcp-ping');

    let arr = ['imap:143', 'imap:993', 'mail:143', 'mail:993'];

    for (let i = 0; i < arr.length; ++i) {

        let alias = arr[i].split(":")[0];
        let port = arr[i].split(":")[1];
        
        tcpp.probe(alias+'.aol.com', parseInt(port), function(err, available) {
            if(available){
                //need break
            }
        });

    }
3
  • This might be helpful : stackoverflow.com/questions/18358481/… Commented Nov 21, 2020 at 3:10
  • 1
    A bit of news for you. For for loop has already finished LONG before your asynchronous callback gets called. So, you can't use this type of structure if you really want to stop the for loop based on an asynchronous result. To understand how this works, your for` loop runs to completion calling all the tcpp.probe() functions. Then, sometime later, long after the for loop has finished, the callbacks you passed to tcpp.probe() will start getting called. To help you with a solution that could work, we need to know what you're really trying to accomplish here. Commented Nov 21, 2020 at 3:12
  • i need to ping ports from array and find one available, then abort the loop Commented Nov 21, 2020 at 3:23

2 Answers 2

1

If you really want to send the probes one at a time, wait to see if you got the right response and if so, stop and if not, go to the next, then it's easiest to use await and promises like this:

const tcpp = require('tcp-ping');
const { promisify } = require('util');
const tcpp_probe = promisify(tcpp.probe);

async function probe(arr) {
    for (const item of arr) {
        const [alias, port] = item.split(":");
        try {
            const available = await tcpp_probe(alias + '.aol.com', parseInt(port));
            if (available) {
                // return whatever you want or act on the result here
                return alias;
            }
        } catch (e) {
            // decide what to do here if you get an error
            // this will log the error and then continue with the loop
            console.log(e);
        }
    }
    // no matches
    return null;
}

And, then you would call it like this:

let arr = ['imap:143', 'imap:993', 'mail:143', 'mail:993'];
probe(arr).then(result => {
    console.log(result);
}).catch(err => {
    console.log(err);
});
Sign up to request clarification or add additional context in comments.

6 Comments

I like your answer because the async/await makes it easy to read/understand. I'm not sure about why you would use try/catch and then switch to .then syntax. Would you not want to perform an action with the result in the body of the if statement in the success block?
@Hyetigran - That's up to you. You didn't say what you wanted to do with the result. I was attempting to make a reusable function that could be called and then the caller could use the result. If you want to just embed the action inside the function you can choose to do that. You didn't show the action so we had no idea what that was.
Thanks for the response, btw I'm not OP. I was asking a follow up question because generally I've been told to use either .then or try/catch. Therefor it was interesting to see it being combined in your example and I wasn't sure if it had extra benefit/util to do so.
@Hyetigran - Well, you can only use await inside an async function so when I call this function from the top level, you can't use await there (until top-level await comes to nodejs) so I'm using .then(). The guidance is to not mix await and .then() in the same function, not that you can't use both in your program for different purposes. The point of await inside the function is that it's way easier to sequence async operations in a for loop with await.
Thanks for the quick answers, but how do you make the function synchronous?
|
0

If you can't use async/await or promises, you could try calling your function recursively.

var probeAll = function(index, callback, err, available){
    // Terminal Condition
    if (available){
        // do something
        callback(available);
        return;
    }
    let arr = ['imap:143', 'imap:993', 'mail:143', 'mail:993'];
    let alias = arr[index].split(":")[0];
    let port = arr[index].split(":")[1];
    
    tcpp.probe(alias+'.aol.com', parseInt(port), function(err, available){
        probeAll(index++, callback, err, available);
    });
};

// First probeAll call
probeAll(0, function(avail){
    // Do something
});

Edit: you very well may need to tweak this to handle errors i.e. if none of the calls come back available, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.