title: Firebase Cloud Functions async function
body: <p>I need some help with async function with firebase and node.js</p>
  
  <p>I have this function in my index.js</p>
  
  <pre><code>const funcBiglietti = require('./biglietti');
      //Region biglietti 
  exports.getBiglietti = functions.https.onRequest((req, res) =&gt; {
  
      let userid = req.url.replace('/',''); 
      let utente = admin.database().ref("Utenti").child(userid).once("value");
  
      var userInfo = {};
  
      utente.then(snap =&gt;{
          if(snap === undefined)
              return res.status(400).send('utente non trovato.');
          else
              return userInfo = JSON.stringify(snap);
  
      }).catch(err =&gt; {
          return res.status(500).send('errore:' + err);
      })
  
      let tickets = await funcBiglietti.getBiglietti(userInfo,userid,admin.database());
      return res.status(200).send(tickets);
  });
  </code></pre>
  
  <p>instead in biglietti.js i have this function:</p>
  
  <pre><code>///Restituisce tutti i biglietti di un utente
  exports.getBiglietti = async function(Utente,IDUtente,database){
  
      console.log('userinfo' + Utente);
      const biglietti = database.ref("Biglietti").child(IDUtente).once("value");
      biglietti.then(snap =&gt;{
          console.log(JSON.stringify(snap));
          return snap;
  
      }).catch(err =&gt; {
          return err;
      })
  }
  </code></pre>
  
  <p>I need the function in index.js to wait the result in biglietti.js but when I'm trying to use the async / await i keep getting:</p>
  
  <pre><code>  deploying functions
  Running command: npm --prefix "$RESOURCE_DIR" run lint
  
  &gt; functions@ lint /Users/Giulio_Serra/Documents/Server Firebase/Hangover/functions
  &gt; eslint .
  
  
  /Users/Giulio_Serra/Documents/Server Firebase/Hangover/functions/biglietti.js
    3:30  error  Parsing error: Unexpected token function
  
  /Users/Giulio_Serra/Documents/Server Firebase/Hangover/functions/locali.js
    13:9  warning  Avoid nesting promises  promise/no-nesting
  
  ✖ 2 problems (1 error, 1 warning)
  
  npm ERR! code ELIFECYCLE
  npm ERR! errno 1
  npm ERR! functions@ lint: `eslint .`
  npm ERR! Exit status 1
  npm ERR!
  npm ERR! Failed at the functions@ lint script.
  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
  </code></pre>
  
  <p>I'm running node v 11.10</p>
  
  <pre><code>node -v
  v11.10.0
  MBP-di-Giulio:~ Giulio_Serra$ 
  </code></pre>
  
  <p>and here is my package.json:</p>
  
  <pre><code>{
    "engines": {"node": "8"},
    "name": "functions",
    "description": "Cloud Functions for Firebase",
    "scripts": {
      "lint": "eslint .",
      "serve": "firebase serve --only functions",
      "shell": "firebase functions:shell",
      "start": "npm run shell",
      "deploy": "firebase deploy --only functions",
      "logs": "firebase functions:log"
    },
    "dependencies": {
      "async": "^2.6.2",
      "firebase-admin": "~5.12.1",
      "firebase-functions": "^2.2.0",
      "request": "^2.88.0"
    },
    "devDependencies": {
      "eslint": "^4.12.0",
      "eslint-plugin-promise": "^3.6.0"
    },
    "private": true
  }
  </code></pre>
  
  <p>What I'm missing to use async functions? I'm a little bit lost. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>resolved by changing code like this:</p>
  
  <pre><code>//Region biglietti 
  exports.getBiglietti = functions.https.onRequest((req, res) =&gt; {
  
      let userid = req.url.replace('/',''); 
      let utente = admin.database().ref("Utenti").child(userid).once("value");
  
      utente.then(snap =&gt;{
          if(snap === undefined)
              return res.status(400).send('utente non trovato.');
          else{
              return funcBiglietti.getBiglietti(snap,userid,admin.database()).then(function(data){
                  return res.status(200).send(data);
              }).catch(err =&gt; {
                  return res.status(500).send('errore:' + err);
              })
          }    
  
      }).catch(err =&gt; {
          return res.status(500).send('errore:' + err);
      })
  });
  </code></pre>