Syntaxerror: Unexpected Token Function - Async Await Nodejs

SyntaxError: Unexpected token function - Async Await Nodejs

Async functions are not supported by Node versions older than version 7.6.

You'll need to transpile your code (e.g. using Babel) to a version of JS that Node understands if you are using an older version.

That said, versions of Node.js which don’t support async functions are now all past End Of Life and are unsupported, so if you are using an earlier version you should very strongly consider upgrading.

Parsing Error: unexpected token function on Async Function with a recent Node version

Good morning,
Do you use eslint ?
if yes ->

{
"parserOptions": {
"ecmaVersion": 2017 @see https://eslint.org/docs/latest/user-guide/configuring/language-options
}

Unexpected token' when recursively calling async function in Nodejs

This is because you have used await outside of an async function (note that it is inside an arrow function!).

const comments = [];
const commentsQuerySnap = await commentsRef.get();

commentsQuerySnap.forEach((comment) => {
let commentData = comment.data();
comments.push(commentData);
if (commentData.commentCount > 0) {
let childComments = await getChildComments(commentData.commentID); // the keyword "await" here is invalid
comments = comments.concat(childComments);
}
});

But you can't just add async to this arrow function, because then your code won't properly wait for the comments array to be filled.

To properly fix this, you need to use .map() on the commentsQuerySnap.docs array in addition to using Promise.all to wait for each comment (and its children) to be retrieved.

const comments = [];
const commentsQuerySnap = await commentsRef.get();

await Promise.all(
commentsQuerySnap.docs.map(
async (comment) => {
let commentData = comment.data();
comments.push(commentData);
if (commentData.commentCount > 0) {
let childComments = await getChildComments(commentData.commentID);
comments = comments.concat(childComments);
}
})
)
);

While that above block works, the comments array may be out of order to what you were expecting. If you must maintain order of the comments fetched so they are in the same order as the query, you should return the built comments array for each document and then flatten them after they all have been retrieved.

// removed const comments = []; here
const commentsQuerySnap = await commentsRef.get();

const arrayOfCommentThreads = await Promise.all(
commentsQuerySnap.docs.map(
async (comment) => {
let commentData = comment.data();
const commentThread = [commentData];
if (commentData.commentCount > 0) {
let childComments = await getChildComments(commentData.commentID);
commentThread = commentThread.concat(childComments);
}
return commentThread;
})
)
);

const comments = arrayOfCommentThreads.flat();

Personally, I prefer the spread operator to using .concat like so:

const commentsQuerySnap = await commentsRef.get();

const arrayOfCommentThreads = await Promise.all(
commentsQuerySnap.docs.map(
async (comment) => {
const commentData = comment.data();
if (commentData.commentCount === 0) {
return [commentData];
}

const childComments = await getChildComments(commentData.commentID);
return [commentData, ...childComments];
})
)
);

const comments = arrayOfCommentThreads.flat();

SyntaxError: Unexpected token function in async function?

I believe you're using a older version of Node. Async functions are not supported by Node versions older than version 7.6. You can check here.

If you want to use async/await then you need to transpile using Babel for your node version.

Edit:

As you said you are using v7.3, you can use (from v7.0 to v7.5) the --harmony flag to enable the experimental features. To know more about the flag, check this out: What does `node --harmony` do?

async function run() { ^^^^^^^^ SyntaxError: Unexpected token function

Async functions are not supported in your version of Node. You really only have two options.

  1. To install Babel, to compile the javascript in a way that can be understood (transpiled)

  2. Upgrade your version of Node

I would HIGHLY recommend to upgrade Node.

How to upgrade node:

https://www.surrealcms.com/blog/how-to-upgrade-or-downgrade-nodejs-using-npm.html

EDIT

I've just come across this SO post from @Quentin - which explains this better. (Kudos) so, I thought I'd include

SyntaxError: Unexpected token function - Async Await Nodejs

(Node J.S.) SyntaxError: Unexpected token function on an async function

async function is supported officially in nodejs 8 and later. you should update your nodejs version to 8 or later.

How to fix 'Unexpected Token' error with async function

The OAuthHelpers class requires 'simple-graph-client' which houses all of the methods you are looking to utilize. In the original sample your code draws from, BotBuilder-Sample 24.bot-authentication-msgraph, if you navigate to the simple-graph-client.js file, you will see the methods called (i.e. sendMail, getRecentMail, getMe, and getManager) in the OAuthHelpers.js file.

If you haven't already, you will need to include a method for creating an event. This, in turn is called from the OAuthHelpers.js file as part of the bot dialog.

It's hard to know what is what without more code, but my guess is the token is being passed into your createevent method but, as the method (likely) doesn't exist as a graph api call, it doesn't know what to do with it.

Check out the following links for guidance:

  • MS Graph sample showing a GET call for top 3 calendar events
  • MS Graph unit test example, but demonstrates an event POST
  • API reference for creating an event
  • Add'l info on creating recurring events...might prove useful

Hope of help!

async arrow function SyntaxError: Unexpected token (

Currently, the default runtime for Cloud Funtions is node 6, which doesn't support async/await. You will need to edit your package.json to target the node 8 runtime, which uses a version of JavaScript that does have async/await.

Set the version by adding an engines field to the package.json file
that was created in your functions/ directory during initialization.
For example, if you prefer to use only version 8, edit package.json to
add this line:

"engines": {"node": "8"}

If you have already deployed a version of the function, you will need to delete it and deploy again after this configuration is in place.

Unexpected Token async ()

There is missing } in the code. Check below

const makeRequest = async() => {  try {    const user = await sails.models.user.findOne({      id: user_id    });    return Promise.resolve(user);  } catch (error) {    sails.log.error('error getting data', error);  }} // -> Its misssing in your codereturn makeRequest().then(out => {  return Promise.resolve(out);});


Related Topics



Leave a reply



Submit