-
-
Notifications
You must be signed in to change notification settings - Fork 9
Add support for await #10
Comments
Hi. |
Hi there, Yes I should've added steps to reproduce already. Here are the steps to reproduce:
<html>
<body>
{{ await (await fetch('https://reqres.in/api/users?page=2')).json() }}
</body>
</html>
Currently, this will throw an error because in render when we execute this JS, we execute it on top level. This is the line of code that executes the code https://github.com/abelljs/abell-renderer/blob/master/src/execute.js#L60 As you can see we're assigning it to a variable called Instead the above snippet should return the json and not throw error. |
Thanks. I'll take a proper look later today. I think I have an idea of what's going on, but I'd rather check first and be sure. |
So I couldn't get your example to work. Then I realised I needed to both install node-fetch and then add the following line to the .abell file: and now I get the following error: This is my entire file
Any ideas why I can't get the example to exit with the correct error? |
Oh yes you will have to |
I made this sandbox, https://codesandbox.io/s/abell-renderer-await-example-w6t9v?file=/src/index.abell You can run |
Okay, so I spent a while having a real hack around with the code here. So what's happening is that the vm is running the code just fine, but obviously it's just running everything synchronously. So at the point where you await the fetch request it's value is a promise (in a pending state). Now, I will point out that this solution doesn't work - as in, it does not render the html as you'd like. The reason for that is that a proper refactor of the code is required to make it support async/await correctly. Firstly in execute.js, I created a new async execute function. Within there we create a promise and once the VM has done its work, we consider the promised resolved. We can await that promise and only then return the result once fulfilled:
Then in index.js I implemented the following at line 76 to intercept when a user uses 'await' in their code, and make it redirect to my executeAsync method:
The console output will give you the actual result from the fetch. What needs to be done from this stage is something akin to the following:
Anyway, I'm sorry I couldn't help implement a full solution, but it did end up taking a lot longer than I anticipated. Hopefully though this puts you on track for a proper solution. |
THAT'S A HUGE HELP @mojiwa !! Thank you so much 🎉 I will try out your snippet probably by tomorrow or the day after. Again thank you so much! |
No problem. I'm glad I could help. |
Node is also releasing support for top level await. That might be relevant here |
This is the next thing that I will pick up. Let me know if you have another idea other than the above discussion |
We should be able to render something like
{{ await myAsyncWork() }}
. Currently, it throws an error saying await should be inside async. Even if we add async the value it will return will be a promise only but to print the content in HTML file we need that value to be the actual value instead of promise.The text was updated successfully, but these errors were encountered: