title: Svelte: how to wait for component render
body: <p>I'm making a simple app based on socket connection. Basically one user is deciding what other users can see. One of functionalities is to start timer for other users. So, there is a Timer component that has timerState variable and startTimer function. Timer component is rendered alond with whole user view when user connects to socket. And reacting to incoming event &quot;timer was started&quot; is easy and works perfectly. Problem starts when timer is already started and user refreshes his browser (or just connects late, when timer was already set).
  Information that timer was set is stored in data object, which is sent to user as welcome event after initial connection to socket.</p>
  <p>To simplify, users view is composed like this:</p>
  <pre><code>{#if !isDataComplete}
  &lt;WaitingComponent&gt;
  {:else}
  &lt;Timer bind:{tools}&gt;
  {/if}
  </code></pre>
  <p>Tools is an object that includes some methods, mainly startTimer method.</p>
  <p>So, after connection, user receives data object, which switches wiev from waiting to app wiev (here: timer, initially empty, but rendered). If there is a timer property, that means a timer was already set and startTimer method should be invoked. But, startTimer method is not accessible yet, because script was faster than render and Timer component is not already there. data is not fetched actually, it comes with &quot;welcome&quot; eventfrom socket, so awaiting data does not do the trick, I need to await for component to render.</p>
  <pre class="lang-js prettyprint-override"><code>
      let isDataComplete = false;
      socket.on('welcome', async (incomingData) =&gt; {
                  isDataComplete = incomingData.isAppReady; // triggers switch view if true 
                  await timeout(500); // my &quot;lame solution&quot; (timeout is just setTimeout wrapped in Promise to make that awaitable)
                  if (data.timer) {
                      const currTime = Date.now();
                      if (currTime &lt; data.timer.start + data.timer.duration) {
                          const remainingTime = Math.floor(
                              (data.timer.start + data.timer.duration - currTime) /
                                  1000
                          );
                          tools.startTimer(remainingTime, true);
                      }
                  }
              });
  </code></pre>
  <p>As you can see: I &quot;solved&quot; that by adding await setTimeout for 500ms in script (before calling startTimer)- that does the trick, component managed to render and method is already accesible. But somehow I feel that is the lame solution, so maybe somebody could help with some other way?</p>
  <p>I simply could put timer component outside the if block, to make it render even before receiving data, but that's not really good idea because of view structure.</p>
  <p>I was trying to store setTimer method somewhere else and import it, but it causes some difficulties with managing state of timer from outside. But it is of course a valid way to try.</p>
  <p>Thanks a lot!
  Przemek</p>