Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda