Skip to content

Instantly share code, notes, and snippets.

@mmrko
Last active Aug 20, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@mmrko 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