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

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

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