Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
JavaScript code runs in node.js but not browser

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

Because You mixing Promises with await? Choice one of. Read about this https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!