title: Firebase Cloud Function (async) not returning result
body: <p>I am rather new to Firebase/Firestore/Cloud functions and been trying a little project where a client app calls a Firebase Cloud Function to generate some random keys (random numbers), adds them to Firestore, and when successfully written, returns these keys to the client app. Kinda like a random number generator.</p>
  
  <p>The function is called correctly by the client (according to Firebase Console), does generate the keys, checks if they exist in the Firestore, and if not adds them. All works up to the part where it should return the result to the client. Here the client never gets the result (the keys array). In fact, the callback in the client app (iOS/Swift) is never called.</p>
  
  <p>I am suspecting the problem lies in the promise being returned? According to the Firebase documentation here, async callables should return a Promise although I am not entirely sure what I am doing is correct <a href="https://firebase.google.com/docs/functions/callable" rel="nofollow noreferrer">https://firebase.google.com/docs/functions/callable</a></p>
  
  <p>Here is the code for the cloud function:</p>
  
  <pre><code>export const generateRandomKeys = functions.https.onCall(async (data, context) =&gt; {
  
    // Read data passed from client
    const numberOfKeys = data.numberOfKeys
    console.log("Number of keys to generate: ", numberOfKeys)
  
    // Generate some keys
    const generatedKeys = KeyMaker.newKeys(numberOfKeys)
  
    try {
  
      const randomkeys = []
  
      // Write keys to DB
      for (const key of generatedKeys) {
        const addedKey = await writeKeyToDB(key)
        randomkeys.push(addedKey)
      }
  
      return Promise.resolve(JSON.stringify(randomkeys))
  
    } catch (error) {
        console.log("Error has occured: ", error)
        throw new Error("An Error has occured: " + error)
    }
  })
  
  
  async function writeKeyToDB(key: string){
  
    try {
      // Check if a document with the same key already exists in the DB
      const docRef = db.collection("randomKeys").doc(key)
      const doc =  await docRef.get()
  
      // Document with same key found!
      if (doc.exists) {
        // Generate a new key and try again
        const newKey = KeyMaker.newKey()
  
        console.log("Will generate a new key and try again!. New key: ", newKey)
        await writeKeyToDB(newKey)
      }
  
      const keyDoc = {
        somefield: somevalue,     
      }
  
      // Write to DB then return result
      await docRef.set(keyDoc)
  
      return Promise.resolve(key)
  
    } catch (error) {
        return Promise.reject(error)
    }
  }
  
  </code></pre>
  
  <p>Client (Swift)</p>
  
  <pre><code>     public static func generateNewRandomNumbers(numberOfKeys: Int) {
  
          FirebaseApp.configure()
  
          let functions = Functions.functions(region: FIRESTORE_REGION)
  
          functions.httpsCallable("generateRandomKeys").call(["numberOfKeys": numberOfKeys]) { (result, error) in
  
              // Nothing here executes                                                    
              print("----------got reply---------")
              if let error = error as NSError? {
                  if error.domain == FunctionsErrorDomain {
                      let code = FunctionsErrorCode(rawValue: error.code)
                      let message = error.localizedDescription
                      print("Error \(String(describing: code)): " + message)
                  }
              }
  
              if let keys = (result?.data as? [String]) {
                  dump(keys)
              }
          }
      }
  
  </code></pre>