Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs/Bluebird - keep getting Unhandled rejection Error

am building a daemon which listen to TCP connection > sends commands > listen for events..

so i decided to use bluebird to get rid of all the callbacks.. but am having an issue... i cant seem to catch a rejected error.... i have no idea whats wrong here is my code

the promise:

function exec(cmd, params, options) {
    return new Promise(function(resolve, reject) {
        server.send(cmd, params || {}, options || [], function (err, res, rawRes) {
            if (err) reject(err.msg);
            resolve(res);
        });
    });
}

the execution:

exec("login", {
    // lOGIN
    client_login_name: conf.user,
    client_login_password: conf.pass
}).then(exec("use", {
    // SELECT SERVER
    sid: 4
})).then(exec("clientupdate", {
    // CHANGE NICKNAME
    client_nickname: conf.nick
})).catch(function (err) {
    log.error(err);
});

the error(server is not running) is the error in the reject(err.msg):

Unhandled rejection Error: server is not running
at Object.ensureErrorObject (D:\DEV\node\a90s\node_modules\bluebird\js\main\util.js:261:20)
at Promise._rejectCallback (D:\DEV\node\a90s\node_modules\bluebird\js\main\promise.js:465:22)
at D:\DEV\node\a90s\node_modules\bluebird\js\main\promise.js:482:17
at Object.cb (D:\DEV\node\a90s\modules\ts3interface.js:20:26)
at LineInputStream.<anonymous> (D:\DEV\node\a90s\node_modules\node-teamspeak\index.js:170:47)
at LineInputStream.emit (events.js:107:17)
at LineInputStream._events.line (D:\DEV\node\a90s\node_modules\node-teamspeak\node_modules\line-input-stream\lib\line-input-stream.js:8:8)
at Array.forEach (native)
at Socket.<anonymous> (D:\DEV\node\a90s\node_modules\node-teamspeak\node_modules\line-input-stream\lib\line-input-stream.js:36:9)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)

Thanks in advance :)

like image 810
Ahmed Avatar asked Dec 14 '22 12:12

Ahmed


1 Answers

You must pass callbacks to .then, not promises (that your exec invocations return).

exec("login", {
    // lOGIN
    client_login_name: conf.user,
    client_login_password: conf.pass
}).then(function(loginresult) {
    // SELECT SERVER
    return exec("use", {
        sid: 4
    });
}).then(function(selectresult) {
    // CHANGE NICKNAME
    return exec("clientupdate", {
        client_nickname: conf.nick
    });
}).catch(function (err) {
    log.error(err);
});
like image 113
Bergi Avatar answered Jan 07 '23 23:01

Bergi