randunel / node-srcds-rcon

Node.JS high-level wrapper for SRCDS's remote console (RCON) https://developer.valvesoftware.com/wiki/RCON
107 stars 18 forks source link

Use in function #12

Open MTRNord opened 8 years ago

MTRNord commented 8 years ago

HI I have following code in module.exports and want to get the awnser from the server returned by the function. How can I do that? I tried to put the return in front of the connect but that did gave me the state of the connection.

  status: function () {
    rcon.connect().then(() => {
        rcon.command('status').then(() => {
            console.log('status check');
        });
    }).then(
        () => rcon.disconnect()
    ).catch(err => {
        console.log('caught', err);
        console.log(err.stack);
    });
  },
randunel commented 8 years ago

The functions return promises. If status is also a promised function, do this:

  status: function () {
    return rcon.connect().then(() => {
        return rcon.command('status').then(result => {
            console.log('status check', result);
        });
    }).then(
        () => rcon.disconnect()
    ).catch(err => {
        console.log('caught', err);
        console.log(err.stack);
    });
  },

If status expects a callback, do this:

  status: function (cb) {
    return rcon.connect().then(() => {
        return rcon.command('status').then(result => {
            console.log('status check', result);
            cb(null, result);
        });
    }).then(
        () => rcon.disconnect()
    ).catch(err => {
        console.log('caught', err);
        console.log(err.stack);
        cb(err);
    });
  },
MTRNord commented 8 years ago

Thanks that worked :)

MTRNord commented 8 years ago

OK it did not. If I call the function status I get Promise { <pending> } and not the result :/

randunel commented 8 years ago

Try to call status like this:

status().then(result => console.log('Got status', result))
MTRNord commented 8 years ago

Now the next "error" is Got status undefined

kdev commented 4 years ago

Can you send that promise based code?