Node: Async keeps giving me an error. Cant deploy my functions to Firebase...

Created on 11 May 2018  Â·  8Comments  Â·  Source: nodejs/node

\=====================================

  • 10.1.0:
  • Windows 10:
  • Vs Code, NodeJs, Javasctipt with async:

I am trying to use this with my Firebase projects with NodeJs in Javascript. ESLint doesnt show any errors either. My problem is also extremely similat to this one:-
https://stackoverflow.com/questions/46776835/firebase-async-function-syntax-error?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

What ever I do my node keeps on throwing an error at the word "async". Where ever in my code async appears it gives me an error, like:
\================

Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:36
async function go() {
      ^^^^^^^^
SyntaxError: Unexpected token function
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at getUserFunction (/var/tmp/worker/worker.js:388:24)

\==========================================
Version: 10.1.0
Platform: 64-bit (Windows)
Subsystem: "Async"
\=========================================

const functions = require('firebase-functions');
 exports.apnaNo1 = functions.https.onRequest((request, response) => {
   go();
    response.send("woohhhoooooo2");
 });

function getCoffee() {
  return new Promise(resolve => {
    setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee
  });
}

async function go() {
  try {
    // but first, coffee
    const coffee = await getCoffee();
    console.log(coffee); // ☕

  } catch (e) {
    console.error(e); // 💩
  }
}

_(edited by @vsemozhetbyt: fix markdown nits)_

wrong repo

Most helpful comment

It sounds like the issue is that the code is not running on a version of Node that supports async/await.

All 8 comments

According to Google Cloud Function docs - the latest supported LTS version for Node.js is 6. To be more precise - Node v6.11.5

The current Node.js version running in Cloud Functions is Node v6.11.5.

Not sure what is the difference between the question on SO and yours.

This issue tracker is for reporting bugs in node core and submitting feature requests for node core.

General help questions should be posted to the nodejs/help issue tracker instead.

Issues with third-party modules, npm, or other tools that use node, should be posted to the appropriate issue tracker for that project, unless it can be proven that the issue is in fact with node core and not the module/tool in question.

It sounds like the issue is that the code is not running on a version of Node that supports async/await.

@LEQADA , thank you so much for that insight.
Does that mean it doesnt matter what environment i develop in because on their cloud they are running a version without "async".... did i now understand this correctly?

@YohanWadia looks like it doesn't matter.
But to be honest I've never used GCloud functions. So it's probably better to ask their support.

I am facing the same issue... any suggestions to solve this issue??

thanks in advance

pranav

@pranavkapoorr they introduced Node.js 8 support (still in beta though according to the documentation). Try to follow the instructions
https://cloud.google.com/functions/docs/concepts/nodejs-8-runtime

@LEQADA ,
It took me time to realise at that moment, though I had installed Node v10, it didnt matter cause the cloud machine was still running v6.... so to make async/await work, I took the TypeScript route. But now I am sure Async/Await will work with NodeV8.x.
I havent tried a recent project, cause my last project, When I urgently required it I had translated that project to TypeScript cause that ran my async await even with the cloud machine running v6

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sandeepks1 picture sandeepks1  Â·  3Comments

dfahlander picture dfahlander  Â·  3Comments

ksushilmaurya picture ksushilmaurya  Â·  3Comments

vsemozhetbyt picture vsemozhetbyt  Â·  3Comments

cong88 picture cong88  Â·  3Comments