SolidJSS
SolidJSโ€ข2y agoโ€ข
7 replies
danchez

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?

function createDocument<T extends DocRecord<T>>(initialDocFn: Accessor<Doc<T>>): CreateDocumentResult<T> {
  const [doc, setDoc] = createSignal(initialDocFn());
  const initialDocId = () => initialDocFn()._id;

  const refreshDoc = async (db: Database, docId = "") => {
    const storedDoc = await db.get<T>(docId).catch(initialDocFn);
    setDoc(() => storedDoc);
  };

  createEffect(() => {
    // This is the problematic instruction. How can I re-express this to properly await it?
    void refreshDoc(database(), initialDocId());
  });
}
Was this page helpful?