How to handle necessary async/await work inside of a createEffect?
I'm building a SolidJS hook library and I have some asynchronous work that needs to happen which is occurring in a
createEffect
. I'm currently trying to write integration tests against my hook library and running into problems because I'm currently not awaiting the Promise result which is conflicting with other things occurirng in test. The way I work around it for testing purposes is literally doing an await sleep(1000)
in test to wait for that asynchronous work to finish.
Is there a better way to write my createEffect
in such a way where I do not have to do a sleep
in test?
5 Replies
This will depend on your implementation. You could use actions or createAsync with cache. A potential way to do it could be:
This is some ideas to your implementation, you will need to adapt to your code. Happy coding!
@AlberTenez What if this is all client side driven? I’m not familiar at all with server actions or createAsync. It would be my first time diving into those ideas
The question coming into my mind is, if everything is client-driven, how do you securely connect to the db from the front end?
Check this out: https://start.solidjs.com/core-concepts/actions and https://start.solidjs.com/core-concepts/data-loading I think will be helpful. In case you are using solid start.
In case you are not using solid start, you could use createResource with and async fn https://www.solidjs.com/docs/latest/api#createresource
or, you could do sth like this in a simpler way:
These are the options I could think of.
SolidJS
Solid is a purely reactive library. It was designed from the ground up with a reactive core. It's influenced by reactive principles developed by previous libraries.
Ah, I see, I’ll expand the code snippet. I should have been clearer. I already have a reference to the database locally. The createDocument hook is returned as part of an outer closure function that provides the database signal. So just assume the DB instance already exists
Think IndexedDB from the browser
And yeah, not using SolidStart with the library (but I would like for it to be able to be used in that context if someone is interested)
Regarding testing, the testing libraries usually have a way to skip forward in time. The resource api is useful is you want to turn a promise into a signal