title: Firebase Database Get All Value In Order Cloud Functions
body: <p>I develop for Firebase Cloud Functions. I have a Firebase Realtime Database like this:</p>
  
  <pre><code>----- myData 
        -------eqewrwrepere (this one is a device token)
                           ---------Lta+sde-fer (this one is a firebase id)
                                    firstvalue  : "a"
                                    secondvalue : "b"
  
                          ----------Qrgd+ad-qdda (this one is second firebase id)
                                    firstvalue  : "c"
                                    secondvalue : "d"
  
        -------eqwerSAsdqe (this one is another device token)
                          ---------Lta+sde-fer (this one is a firebase id)
                                   firstvalue  : "x"
                                   secondvalue : "y"
  
                         ----------Qrgd+ad-qdda (this one is second firebase id)
                                   firstvalue  : "z"
                                   secondvalue : "t"
  </code></pre>
  
  <p>I fetch these data by this code.  With this code i fetch all data and put them an array. And when fetching done, i loop this array for finding items. I am an iOS developer, so i am a newbie for NodeJS. Here is what i want to do:</p>
  
  <ol>
  <li>Get <code>firstvalue</code> for each database data.</li>
  <li>Make a api request with firstvalue of each database data.</li>
  <li>Api returns an image.</li>
  <li>Write image temp directory.</li>
  <li>Process this image for visionApi.</li>
  <li>Extract text.</li>
  <li>Update database.</li>
  <li>Send notification for deviceToken</li>
  </ol>
  
  <p>Now i am able to retrieve database items in my array. When i make a request in for loop, request called async. So for loop continues, but request response or writing file and vision processing executed only once. </p>
  
  <p>In for loop, get databasearray[0], make request, write file, process it with vision api, update database and go for next databasearray[1] item.</p>
  
  <p>I read about <code>Promises</code> on different pages. But i did not understand.</p>
  
  <p>Thank you.</p>
  
  <pre><code>'use strict';
  
  const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  var request = require('request');
  var fs = require('fs');
  //var fs = require("fs");
  // Get a reference to the Cloud Vision API component
  const Vision = require('@google-cloud/vision');
  const vision = new Vision.ImageAnnotatorClient();
  // Imports the Google Cloud client library
  //const {Storage} = require('@google-cloud/storage');
  var fs = require("fs");
  var os = require("os");
  var databaseArray = [];
  
  
  exports.hourly_job = functions.pubsub
  .topic('hourly-job')
  .onPublish((event) =&gt; {
      console.log("Hourly Job");
  
  
      var db = admin.database();
      var ref = db.ref("myData")
  
  
      ref.once("value").then(function(allData) {
  
          allData.forEach(function(deviceToken) {
  
              deviceToken.forEach(function(firebaseIDs) {
  
                  var deviceTokenVar = deviceToken.key;
                  var firebaseIDVar = firebaseIDs.key;
                  var firstvalue = firebaseIDs.child("firstvalue").val();
                  var secondvalue = firebaseIDs.child("secondvalue").val();
  
  
                  var items = [deviceTokenVar, firebaseIDVar, firstvalue, secondvalue];
  
                  databaseArray.push([...items]);
  
  
              });
  
          });
          return databaseArray;
      }).then(function(databasem) {
  
          var i;
          for (i = 0; i &lt; databaseArray.length; i++) {
  
              var databaseArrayDeviceToken = databaseArray[i][0];
              console.log("DeviceToken: " + databaseArrayDeviceToken);
  
              var databaseArrayFirebaseID = databaseArray[i][1];
              console.log("FirebaseID: " + databaseArrayFirebaseID);
  
              var databaseArrayfirstvalue = databaseArray[i][2];
              console.log("firstval: " + databaseArrayfirstvalue);
  
              var databaseArraysecondval = databaseArray[i][3];
              console.log("Second: " + databaseArraysecondval);
  
  
              var url = "http://api.blabla" + databaseArrayfirstvalue;
  
              /////////////here make a request, pause loop, process returned image, but how //////////////////////         
              request.get({
                      url: url,
                      encoding: 'binary'
                  }, function(error, httpResponse, body) {
  
  
                      if (!error &amp;&amp; httpResponse.statusCode == 200) {
  
                          fs.writeFileSync('/tmp/processed.jpg', body, 'binary')
                          console.log("file written");
                      })
  
              }
  
          });
  
          return true;
      });
  </code></pre>