Table of Contents
Have you ever seen the notices “SyntaxError: Unexpected reserved word ‘await’” below when you open the firebase function? If yes and you don’t feel confident doing it yourself. Our recommendations in this article are really suitable for you to correct them.
How Does the SyntaxError: Unexpected reserved word ‘await’ Happen?
Here is a sample program running that gave the above error:
exports.deleteUser = functions.https.onCall(async(data, context) => {
let id = context.auth.uid;
console.log('Delete user: ' + id);
//delete from algolia
usersIndex.deleteObject(id);
console.log(id + 'Deleted from algolia');
//delete user following
await admin.firestore().collection('users').doc(id).collection('Following').get()
.then(async(snapshot) => {
for await (const document of snapshot.docs) {
await admin.firestore().collection('users').doc(document.documentId)
.update({
'NumberOfFollowers': FieldValue.increment(-1)
});
await admin.firestore().collection('users').doc(document.documentId).collection('Followers')
.doc(id).delete();
}
return console.log('Following of ' + id + ' deleted');
});
...
And the error is displayed on the system like this:
! functions[deleteUser(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /srv/index.js:47
for await (const document of snapshot.docs) {
^^^^^
SyntaxError: Unexpected reserved word
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at getUserFunction (/worker/worker.js:439:24)
Cause of error
It is a “unexpected reserved word wait” error can occur when the await keyword is utilized within a function which was not designated as in asynchronous mode. To utilize the await keyword within an operation make sure to mark the function that is directly enclosed by the function as async.
How to fix it?
Here are some solutions for your reference:
Approach 1: Using async()
In order for the await function to work, first declare the async function, using the following program:
const functionReturningPromise = (input) => {
return new Promise((resolve, reject) => {
if (!input) {
return reject();
}
return resolve();
});
}
const functionAwaitingPromise = async () => {
for (let i = 0; i < 20; i +=1) {
try {
await functionReturningPromise(i);
console.log(i);
} catch (error) {
console.log(error);
}
}
}
Approach 2: Check the version in package.json
The requirement for for-await loops in node is version 10 locally or in package.json. So you should check the version in package.json and appear if it is node 10 as follows:
"engines": {
"node": "10"
}
Furthermore, cross-check with local notes and the target will be displayed as the following text:
$ node --version
...should print 10.x.x or later
Conclusion
In conclusion, we’ve shown you how to handle SyntaxError: Unexpected reserved word ‘await’ . Hope they will be useful to you. If you have any difficulties during the operation, please contact us for answers. Thank you for reading!
Read more:
to use
await
the function within it must be async. Based on your comments the addition ofthe async
to the function will fix the issue. I’ll share this:Possibly, you could remove the async from the outer function because it does not contain any asynchronous operations, but that would depend on whether the caller of that sendVerificationEmail is expecting sendVerificationEmail to return a promise or not.
I found it in the other forum and I think This is the best way to stabilize your problem. Just copy it