Root + Async = Headaches
See original GitHub issueIs there a good way to gracefully handle disposals where I have to build up values or data signals from within async/await code? E.g. loading an image asynchronously and then attaching some styles to the element afterward that create and use signals.
It would be nice to have something akin to Objective-C’s non-ARC autopool that acted similar to S.root()
but as a bit more manual - for example, something like:
const sroot = S.root();
try {
await myCode();
} finally {
sroot.release();
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
async/await is the wrong abstraction - LogRocket Blog
What we are trying to do with async/await is to ignore reality and have these async operations appear to be happening synchronously.
Read more >.net - Async property in c# - Stack Overflow
Inside the getter, I need to make sure the root folder exists,otherwise create it. But the CreateFolderAsync is an async method, here I...
Read more >Describe block with async function behaving weirdly #2975
This causes major headaches otherwise.
Read more >Sync vs. Async Python: What Is the Difference? - Hacker News
The recent innovations in asynchronous programming involve ways to signal to the programming language that the current task is blocked, thereby ...
Read more >How To Handle Async Data Loading, Lazy Loading, and Code ...
Notice that the component renders before the data is loaded. The advantage with asynchronous code is that it won't block the initial render....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have a soft spot for making simple games. I usually use surplus-toys to avoid the need for a build system and keep them single-file.
It’s often important in S apps to avoid implicit state – application state which isn’t contained in a data signal and isn’t immediately addressable. In your app, the implicit state is whether the tile image has loaded yet. If we were to ask “has the tile loaded yet?”, we can’t really answer it. Instead, you’re using async/await to say “we won’t run this code until it has.” So the state is encoded into your execution order, which means it also has to propagate all the way up the call tree.
My advice would be to make that state explicit, by making an image data signal that starts as
null
and then is populated once it has arrived.I just pulled and made the changes locally. Where you did:
… I changed to …
Since
img
was now a signal, I changed{img}
to{img()}
inmakeTile(img)
.Then strip all the
async
andawait
calls from the file.I’m a build systems nerd - this is one I built up for another app that I just stripped out for this small little exercise 😃 Seems overkill, because it is haha. It’s part of a much larger project 😉 surplus-toys is super neat though!
Derp, that’s so obvious now. I think this is also a side effect of me mixing logic with my display code, which I did here for time’s sake but wouldn’t normally do.
Thanks, this is much better.