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

191
Vistas
fetch constantly returns Promise {<pending>}

I decided to create a special service aka ES6 class with needed functions to get data from API and then in index.js I could create an instance of the class and work with it. Unfortunately, when I try it, it always returns

Promise {<pending>}

and I don't really know what to do.

nytService.js:

export default class NYTService {
    urlBase = "https://api.nytimes.com/svc/topstories/v2/";
    category = "world";
    apikey = *my api key* ;

    async getNews(category = this.category) {
        let url = `${this.urlBase}${category}.json?api-key=${this.apikey}`;

        let res = await fetch(url)
        let data = await res.json();
        
        return data;
    }
}

index.js:

import NYTService from "../services/nytService.js";

let nytService = new NYTService();

async function getNewsFinally() {
    let res = await nytService.getNews();

    return res;
}

console.log(getNewsFinally());

I did tried different things with the getNewsFinally function, did various .then chains, nothing helped

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

See if this tells you why:

    async getNews(category = this.category) {
        let url = `${this.urlBase}${category}.json?api-key=${this.apikey}`;

        try {
           let res = await fetch(url)
      
           if (!res.ok) {
               return res.statusText;
           }
           
           return await res.json();
        } catch(err)  {
           console.error(err);
           throw new Error(err);
        }
      }
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