title: Is there a way to initialize an Xstate state machine using a promise returned from Firebase?
body: <p>I am trying to persist state using Firebase. I am currently using a 'saveState' function which works fine and properly saves the most recent state to Firebase.</p>
  <p>Now I'd like to be able to initialize the state machine based on the most recent saved state in Firebase. In the code below I am trying to use my 'loadState' function to provide Xstate with a config object. It currently returns a promise with the correct state configuration within.</p>
  <p>Here is my 'saveState' code:</p>
  <pre><code> //This function works fine.
   function saveState(current, id){
          let transactionJSON = serialize(current);
          transactionJSON['createdOn'] = new Date();
          return firebase.saveTransactionState({transactionJSON, transactionId:id});
      }
  </code></pre>
  <p>Here is my 'loadState' function which returns a promise from Firebase with the correct config information within.</p>
  <pre><code>function loadState(id){
          return firebase.getTransactionState({transactionId:id}).then(function(querySnapshot) {
              return querySnapshot.docs.map(doc =&gt; deserialize({...doc.data()})  );
          });
      };
  </code></pre>
  <p>Now my issue is trying to load Xstate with the above 'loadState' function. Here I am trying to use a useMachine React hook:</p>
  <pre><code>const [current, send] = useMachine(transactionMachine,{
          state: () =&gt; loadState(id), //Trying to do something like this, but it doesn't seem to work.
          actions:{
              save: () =&gt; saveState(current, id),
          },
      });
  </code></pre>
  <p>I end up with the error: &quot;TypeError: Cannot read property 'data' of undefined&quot;, which I believe is happening because the promise hasn't resolved yet leading to trying to read an undefined value.</p>
  <p>Is this even possible or am I going about this all wrong?</p>
  <p>I am new to all this, any guidance would be appreciated. Thank you.</p>