title: Firestore cloud function asynchronous execution with promise
body: <p>I have orders collection and products collection in my application. The user can have multiple products in their single order. What I want to do is calculating the amount of each product reading through products collection and then perform the further action. Below is what I got as of now.</p>
  
  <pre><code>exports.myfunc = functions.firestore.document('collection/{collid}')
      .onCreate(event =&gt; {
          let data = event.data.data();
          const products = data.products;
          const prices = [];
          _.each(products, (data1, index) =&gt; {
              const weight = data1.weight;
              const isLess = data1.isLess;
              firebaseAdmin.firestore().collection('collection').doc(data1.productId).onSnapshot(data2 =&gt; {
                  let amount = weight === '1/2' ? data2.data().price1 : data2.data().price1 * weight;
                  amount += isLess ? 50 : 0;
                  prices.push(amount);
              });
          });
          //Do some task after _.each with new total
  
  });
  </code></pre>
  
  <p>But am not able to achieve synchronous task here, so that I can store actual amount for the product against its order and calculate total to store in document.</p>
  
  <p>Could anyone please tell me how I achieve the above-said scenarios? How I can work along with <code>promise</code> and then <code>callback</code>?</p>