make it work with variables too

This commit is contained in:
Jonathan Rainville 2018-08-22 14:09:10 -04:00
parent 8ebe55f457
commit a0b06c6197
1 changed files with 13 additions and 3 deletions

View File

@ -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()) {