Skip to content

Instantly share code, notes, and snippets.

@mmrko
Last active June 20, 2023 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mmrko/d3598895e523b58f088b0d196c0ad5e1 to your computer and use it in GitHub Desktop.
Save mmrko/d3598895e523b58f088b0d196c0ad5e1 to your computer and use it in GitHub Desktop.
Top-level await support for TypeScript REPL
// ... 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)
}
})
}
@mmrko
Copy link
Author

mmrko commented Aug 20, 2020

Or just use node --experimental-repl-await -r ts-node/register directly instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment