From a0b06c61979bf0ff06b575ae769f756bb4d2eb20 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 22 Aug 2018 14:09:10 -0400 Subject: [PATCH] make it work with variables too --- lib/core/modules/coderunner/codeRunner.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 95bc5b40..9b965e69 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -41,9 +41,19 @@ class CodeRunner { if (!cb) { cb = function() {}; } - if (code.startsWith('await')) { - code = code.substring(5); // remove await keyword - code += '.then(console.log).catch(console.error)'; // Add promise catch + const awaitIdx = code.indexOf('await'); + if (awaitIdx > -1) { + let end = code.length; + if (code[end - 1] === ';') { + end--; // Remove the `;` because we add function calls + } + if (awaitIdx < 2) { + code = code.substring(5, end); // remove await keyword + code += '.then(console.log).catch(console.error);'; // Add promise catch + } else { + code = code.substring(0, end); // remove ending `;` + code = `(async function() {${code}.then(console.log);})();`; + } } let result = RunCode.doEval(code); if (forConsoleOnly && self.ipc.isServer()) {