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

185
Views
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 answers
Answer question

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 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!