Last active
June 20, 2023 08:08
-
-
Save mmrko/d3598895e523b58f088b0d196c0ad5e1 to your computer and use it in GitHub Desktop.
Top-level await support for TypeScript REPL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... by wrapping the executed statement in an async IIFE | |
const repl = require("repl") | |
const wrapInAsync = (cmd) => { | |
if (!cmd.trim()) return "" | |
return `;(async () => { | |
return ( | |
${cmd} | |
) | |
})()` | |
} | |
const replInstance = repl.start() | |
const defaultEval = replInstance.eval | |
replInstance.eval = (cmd, context, filename, callback) => { | |
defaultEval(wrapInAsync(cmd), context, filename, async (error, result) => { | |
if (error) return callback(error) | |
try { | |
callback(null, await Promise.resolve(result)) | |
} catch (e) { | |
callback(e) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or just use
node --experimental-repl-await -r ts-node/register
directly instead.