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

158
Views
TS ¿Por qué no configuro una matriz en Promise en fetch?

Aprendo TS, y he visto este código. Hay un código como este:

 type FetchPokemonResult<T> = T extends undefined ? Promise<PokemonResults> : void;

Devuelve una matriz de datos json. Entonces, ¿por qué eso no es Promise<PokemonResults[]> ? ¿Por qué es sin una matriz?

código

 interface PokeArr { name: string; url: string; } interface PokemonResults { count: number; next: string; previous: null; results: PokeArr[]; }; type FetchPokemonResult<T> = T extends undefined ? Promise<PokemonResults> : void; function fetchPokemon<T extends undefined | ((data: PokemonResults) => void)>(url: string, cb?: T): FetchPokemonResult<T> { if(cb) { fetch(url) .then(res => res.json()) .then(cb) return undefined as FetchPokemonResult<T>; } else { return fetch(url).then(res => res.json()) as FetchPokemonResult<T> } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Se escribe de esta manera porque la función admite proporcionar una devolución de llamada cb o devolver una promesa.

 // typeof e is PokemonResults fetchPokemon("", (e => console.log(e))); // typeof e is PokemonResults fetchPokemon("")?.then(e => console.log(e));

Devuelve una matriz de datos json. Entonces, ¿por qué eso no es Promise<PokemonResults[]> ? ¿Por qué es sin una matriz?

No parece devolver una matriz. Según su pregunta, devuelve un PokemonResults que tiene una lista de results .

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!