Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: async generator functions / async components #3

Open
gustavopch opened this issue Jun 16, 2022 · 2 comments
Open

feat: async generator functions / async components #3

gustavopch opened this issue Jun 16, 2022 · 2 comments

Comments

@gustavopch
Copy link

Hey, thanks for your work on this lib.

Crank.js allows components to be defined as async generator functions. For example:

async function IPAddress () {
  const res = await fetch("https://api.ipify.org");
  const address = await res.text();
  return <div>Your IP Address: {address}</div>;
}

await renderer.render(<IPAddress />, document.body);
console.log(document.body.innerHTML); // <div>Your IP Address: 127.0.0.1</div>

Can Hacky support it too? It seems it would be mostly a matter of adding await in some parts of the code.

@aidenybai
Copy link
Owner

This would be cool to support, but I'm not exactly sure how to implement this at the moment. I'll look into how Crank.js implements this feature.

@gustavopch
Copy link
Author

gustavopch commented Jun 16, 2022

@aidenybai I'd imagine something like this applied to https://github.com/aidenybai/hacky/blob/105fa12777990b7680c4964beea142392a772bc9/src/component.ts and then likely other changes to add await to data.diff() where it's called:

-  data.diff = () => {
+  data.diff = async () => {
-    const next = <VNode | VNode[]>component.next().value;
+    const next = await (await <VNode | VNode[]>component.next()).value;
     if (Array.isArray(next)) {
       return <VNode>h('_', undefined, ...next);
     } else {
       return <VNode>next;
     }
   };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants