Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 764

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 764

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 580

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Fix the Error “SyntaxError: Unexpected reserved word 'await'” - TutoPal

Fix the Error “SyntaxError: Unexpected reserved word ‘await’”

When you are working with JavaScript programs, it is normal to have encountered errors. As it is a versatile language, there are infinite projects that programmers and developers are working on. Beginners are also practicing various projects to become a pro in JavaScript programming. You may encounter the exception “SyntaxError: Unexpected reserved word ‘await’”.

We provide you with valuable yet simple to execute solutions to help you get rid of the error warning. Let’s check out how the error occurs

How the error pops up

The error occurs when you execute the firebase function. Have a look at the program code

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');
});
...

After executing the program, you get the error 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)

How to Fix the Error “SyntaxError: Unexpected reserved word ‘await’”

The cause of the error is the use of ‘await’ keyword inside a function that was not in asynchronous mode. In order to use it within an operation, you need to ensure to do function marking, which is encircled by the async function.

Check out the solutions to fix the error

Solution 1 – Declare async()

To remove the error, the await function needs to work properly. For this, you need to declare the async function. Use the following code

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);
}
}
}

Solution 2 – Confirm the package.json version

You probably end up with the error warning if you are using an older version of the node in package.json or locally. To avoid the error warning, make sure to at least have node 10. To confirm which version you have, follow the below code

"engines": {
    "node": "10"
}

To check if you have a local node, follow the below command

$ node --version
...should print 10.x.x or later

Conclusion

We have discussed how to solve the error “SyntaxError: Unexpected reserved word ‘await’”. The solutions we mentioned fix the error.

I hope you find it helpful!

Reference Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

https://stackoverflow.com/questions/42299594/await-is-a-reserved-word-error-inside-async-function

Leave a Reply

Your email address will not be published. Required fields are marked *