node-srcds-rcon icon indicating copy to clipboard operation
node-srcds-rcon copied to clipboard

Use in function

Open MTRNord opened this issue 9 years ago • 6 comments

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);
    });
  },

MTRNord avatar Mar 23 '16 14:03 MTRNord

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);
    });
  },

randunel avatar Mar 24 '16 08:03 randunel

Thanks that worked :)

MTRNord avatar Mar 24 '16 10:03 MTRNord

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

MTRNord avatar Mar 24 '16 11:03 MTRNord

Try to call status like this:

status().then(result => console.log('Got status', result))

randunel avatar Mar 24 '16 15:03 randunel

Now the next "error" is Got status undefined

MTRNord avatar Mar 24 '16 16:03 MTRNord

Can you send that promise based code?

kdev avatar Apr 19 '20 02:04 kdev