I don't know what is wrong with this code. It throws an error in the console that says :
Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
Can anyone tell me what's wrong with my usage of await?
let user = {
name: 'George',
surname: 'Arqael'
};
let regUser = await fetch(url, {
method: 'post',
headers: {
'Content-type': 'application/json; charset=utf-8'
},
body: JSON.stringify(user)
});
let result = await regUser.json();
console.log(result);
awaitcan only be used in anasyncfunction. Whatever the surrounding function is, mark it asasync.