0

Sorry if this is due to a misunderstanding of how callbacks work, but I'm new to Node and really need to return useful error messages, but at the moment return a success followed later by the error which is caught:

connectDevice: function (ip) {
        var port = 5555; 

        console.log("Connecting to device " + ip + ":5555")
        client.connect(ip, port)
        .then(function() {
            return false; // wait until no errors confirmed
        })
        .catch(function(err) {
            if (err === null) {
                return false; 
            } else {
                if (debug) console.error('Could not connect to device at IP ' + ip, err.stack);
                console.log('Could not connect to device');
                return true; // return error = true 
            }
        });
    },

I'm trying to connect to an android device using adbkit which uses promises, however I can't figure out how to adapt them from examples as there's no parent function.

It's important it waits to confirm a connecion before sending commands however. I get this is because of Node's event driven nature but don't know enough how to properly deal with this situation.

Thanks for any help and patience.

  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jonrsharpe Jun 08 '16 at 21:40
  • You may need to see this first. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – Jose Hermosilla Rodrigo Jun 08 '16 at 21:46

0 Answers0