Last active
June 13, 2024 06:35
-
-
Save benqus/82ff5bc8bcb9f66e11aaff61b5864476 to your computer and use it in GitHub Desktop.
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
async function render(...args: Array<any>) { | |
const strings = args[0]; | |
const params = args.slice(1); | |
const results = await Promise.allSettled(params.map(param => typeof param === 'function' ? param() : param)); | |
return strings.map((str: string, i: number): string => { | |
const result = results[i]; | |
if (result == null) return str; | |
return str + (result.status === 'fulfilled' ? result.value : result.reason); | |
}).join(''); | |
} | |
const variable = 5; | |
const result = await render` | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>${() => 'Title'}</title> | |
</head> | |
<body> | |
<script type="module" src="index.ts"></script> | |
<h1>${async () => new Promise(res => setTimeout(() => res('Hello World!'), 1000))}</h1> | |
<h2>${async () => new Promise((_, rej) => setTimeout(() => rej('error'), 1500))}</h2> | |
<h3>${variable}</h3> | |
</body> | |
</html> | |
`; | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment