fix: socket.io methods calling callbacks twice if method returns promise

This commit is contained in:
Barış Soner Uşaklı
2019-07-19 16:46:40 -04:00
parent d43f5dcf4c
commit 44a3352003
2 changed files with 17 additions and 9 deletions

View File

@@ -131,7 +131,6 @@ function onMessage(socket, payload) {
return socket.disconnect();
}
var cbCalled = false;
async.waterfall([
function (next) {
checkMaintenance(socket, next);
@@ -147,19 +146,20 @@ function onMessage(socket, payload) {
}
},
function (next) {
const returned = methodToCall(socket, params, next);
let callbackCalled = false;
function nextOnce(err, res) {
if (callbackCalled) { return; }
callbackCalled = true;
next(err, res);
}
const returned = methodToCall(socket, params, nextOnce);
if (returned && typeof returned.then === 'function') {
returned.then((payload) => {
next(null, payload);
nextOnce(null, payload);
}, next);
}
},
], function (err, result) {
if (cbCalled) {
return;
}
cbCalled = true;
callback(err ? { message: err.message } : null, result);
});
}