eta-dev / eta

Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
https://eta.js.org
MIT License
1.35k stars 60 forks source link

renderStringAsync not working in Angular 17 #286

Closed vjm14 closed 2 months ago

vjm14 commented 2 months ago

Describe the bug We are trying to use renderStringAsync function to execute a custom function with an await, but we always get an error.

We are using Angular 17.

To Reproduce

const eta = new Eta({
                autoEscape: false,
                tags: ["{{", "}}"],
                varName: 'listOf',
                useWith: true
});

const result = eta.renderStringAsync(`Hello {{= await  hello(test1) }} \n\n {{= await world(test2) }}`, {
          test1: "hello",
          test2: "world",
          hello: (e) => Promise.resolve(e),
          world: (e) => Promise.resolve(e)
})

Error encountered: error

Expected behavior Expecting to behave the same in the documentation. image

Package & Environment Details

nebrelbug commented 2 months ago

@vjm14 hmm, this is a bit confusing. It's working when I try your code in a clean environment. Could it be something about Angular?

See https://runkit.com/embed/vlwyx6jco93r

vjm14 commented 2 months ago

@nebrelbug I appreciate you looking at this. We're simply switching over to ETA from squirrelly at the moment. We can run the async await function in angular without any problems when using squirrelly, but we are unable to do so using ETA.

vjm14 commented 2 months ago

@nebrelbug We tried to debug the ETA and this is what we see. Since async is set to true, the "ctor" is being set as a Function, not as a AsyncFunction. Do you have any suggestions or alternatives that i can do? MicrosoftTeams-image (3)

vjm14 commented 2 months ago

@nebrelbug I think we resolve the issue. We tried to copy the AsyncFunction in squirrelly and replace it in ETA and it works. We don't get the error anymore.

ETA:
 const AsyncFunction = async function () {}.constructor; // Not working in Angular

Squirrelly:
const AsyncFunction = new Function('return (async function(){}).constructor')(); // Working in Angular
nebrelbug commented 2 months ago

@vjm14 glad to hear you solved it! I'm guessing it has to do with some perplexity of Angular's execution environment, where async works in one context but not in another. I'll close this as resolved, since I don't think this is a common issue.