visit
const render = async (x, ...values) => {
var rendered = "";
for (let u = 0; u < x.length; u++) {
rendered = rendered.concat(x[u]);
if (u < x.length - 1) {
if (typeof values[u] == "function") {
var res = await values[u]()
if(res) rendered += res;
} else {
rendered +=values[u]
}
}
}
return rendered;
};
const result = render`My string is = ${()=> {return (1 + 1)}}`
console.log(result)
// output -> My string is 2
Promise.all()
await render`
Here is my headings:
${() => {
return "".concat(
...[1,2,3,4,5].map((t) => {
return `<h1>My number is: ${t}</h1>`
}),
);
}}
// output: Here is my headings:
// <h1>My number is: 1</h1><h1>My number is: 2</h1><h1>My number is: 3</h1><h1>My number is: 4</h1><h1>My number is: 5</h1>
await render`
Here is my data
${async () => {
const text = `SELECT * FROM table`;
const values = [];
var record = await pool.query(text, values);
return "".concat(
...(await Promise.all(
record.rows.map((t) => {
return `<h1>${t.data}</h1>`
}),
)),
);
}}
`
await render`
Here is my data
${async () => {
const res = fetch("//example.com", {method:"GET"})
const data = await res.json()
return "".concat(
...(await Promise.all(
data.first.map((t) => {
return `<h1>${t.data}</h1>`
}),
)),
);
}}
`