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

145
Vistas
Mapping obsevable to another list of promise observable

Here what i am trying to do: I have pokemons from the api. pokeapi The http response is list of object only includes name and url prop. I need to map these url to observable values. I can basically use

    Promise.all(...).then(data => observable$.next(data))

but this seems unelegant to me, here is what i try

    const __URL__ = 'https://pokeapi.co/api/v2/pokemon/';
    const pokemon$ = from(
        fetch(__URL__)
            .then((res) => res.json())
            .then((res) => res.results)
    );
    
    var pokemons$ = new BehaviorSubject([]);
    pokemon$.subscribe((pokes) => {
        Promise.all(pokes.map((p) => fetch(p.url).then((res) => res.json()))).then((pokks) =>
            pokemons$.next(pokks)
        );
    });

I just wonder is there a method to use observable operators(map) to generate the result with a single observable elegantly.

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

0

You can achieve that using forkJoin function, like the following:

// import { BehaviorSubject, forkJoin, from } from 'rxjs';
// import { mergeMap } from 'rxjs/operators';

const __URL__ = 'https://pokeapi.co/api/v2/pokemon/';
const pokemon$ = from(
  fetch(__URL__)
    .then((res) => res.json())
    .then((res) => res.results as Array<{ name: string; url: string }>)
);

const pokemons$ = new BehaviorSubject([]);
pokemon$
  .pipe(
    mergeMap((pokes) =>
      forkJoin(
        pokes.map((poke) => from(fetch(poke.url).then((res) => res.json())))
      )
    )
  )
  .subscribe((pokks) => pokemons$.next(pokks));
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