title: Firebase cloud function not doing anything with Xstate
body: <p>I'm trying to write a cloud function on Firebase that updates a document in Firebase when another one is written. I use the trigger method onWrite for this. I use Xstate for this, as my original code is more complex, but the idea is the same (and the problem as well). This is the code I use (Typescript):</p>
  
  <pre><code>import * as functions from "firebase-functions";
  import * as admin from "firebase-admin";
  import { Machine, interpret } from "xstate";
  
  admin.initializeApp({});
  
  // This function will trigger as soon as a product of a company owner gets updated.
  exports.productsOnUpdate = functions.firestore
    .document(`companies/{companyId}/{products}/{productId}`)
    .onWrite((change: any) =&gt; {
  
      let done = false;
  
      const PromiseFunction = (docIn: any) =&gt; {
        console.log(docIn);
        return admin
          .firestore()
          .collection("test")
          .doc("testdoc")
          .set({ products: docIn.products }, { merge: true })
          .then((doc: FirebaseFirestore.WriteResult) =&gt; doc.writeTime);
      };
  
      const myMachine = Machine&lt;any&gt;({
        id: "myMachine",
        initial: "idle",
        context: { doc: { products: "a product" } },
        states: {
          idle: {
            on: {
              INIT: "init"
            }
          },
          init: {
            invoke: {
              id: "setDoc",
              src: (context, event) =&gt; PromiseFunction(context.doc),
              onDone: {
                target: "success"
              },
              onError: {
                target: "failure"
              }
            }
          },
  
          success: {},
          failure: {}
        }
      }); // end of machine
  
      const MyMachine = interpret(myMachine).onTransition(state =&gt; {
        console.log(state.value);
        // actually do something here
        switch (state.value) {
          case "INIT":
            break;
  
          case "success":
            done = true;
            console.log("Success");
  
          case "failure":
            console.log("Something went wrong");
  
          default:
            break;
        }
      });
  
      MyMachine.start();
      MyMachine.send("INIT");
      while (done === false);
      return "ok";
    });
  </code></pre>
  
  <p>So, when trying to update a document in the subCollection 'products', this should trigger the function. In the log I see the following:</p>
  
  <p><a href="https://i.stack.imgur.com/XrR1C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XrR1C.png" alt="function log"></a></p>
  
  <p>Absolutely nothing happens. When I make a small error in the context of MyMachine (change <code>context: { doc: { products: "a product" } }</code>, to  <code>context: { doc: { product: "a product" } },</code> I do see this:</p>
  
  <p><a href="https://i.stack.imgur.com/UBvCX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UBvCX.png" alt="enter image description here"></a></p>
  
  <p>So there seems to be something wrong with the promise handling or so. I already spent a day on this; any help is appreciated!</p>