From aee47b299a73742bff956cf0849ebec5233fed96 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 10 Jun 2019 12:06:26 -0400 Subject: [PATCH] feat: awaitable websockets (#7645) * feat: awaitable websockets Adding in conditionals to check the method to call, and handling it as a promise vs. a regular function depending on whether the method itself is an asynchronous function. * fix: switch to .then-checking for awaitable check * fix: proper use of .then check --- src/socket.io/index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 91d660317d..1e101953ba 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -131,6 +131,7 @@ function onMessage(socket, payload) { return socket.disconnect(); } + var cbCalled = false; async.waterfall([ function (next) { checkMaintenance(socket, next); @@ -146,9 +147,19 @@ function onMessage(socket, payload) { } }, function (next) { - methodToCall(socket, params, next); + const returned = methodToCall(socket, params, next); + if (returned && typeof returned.then === 'function') { + returned.then((payload) => { + next(null, payload); + }, next); + } }, ], function (err, result) { + if (cbCalled) { + return; + } + + cbCalled = true; callback(err ? { message: err.message } : null, result); }); }