Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda