I have this code:
async function my_function() {
console.log("Hello");
let response = await fetch(url, {
mode: 'no-cors'
})
.then(response => response.json()) // THIS LINE
.then(response => {
console.log(response);
})
}
which works great if I include const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args)); in node.js. This however, does not run in the browser. When I run this code in google chrome I get this error
Uncaught (in promise) SyntaxError: Unexpected end of input
relating to the
.then(response => response.json()) // THIS LINE
line. What am I doing wrong?
For the browser, you have to enable the cors-policy otherwise it will not works. So in your API, you must include:
"Access-Control-Allow-Origin" : "*",
And also disable the no-cors mode.
Because You mixing Promises with await? Choice one of. Read about this https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await