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

212
Views
Use RxJS pipe () para convertir la matriz en una secuencia de valores asíncronos en Angular
type Movie = {id: string}; type FullMovie = {id: string, picture: string};

Tengo una url que devuelve una matriz de tipo Movie :

 http.get(url).subscribe(res: Movie[])

Uso http.get(movie.id) para cada película de la matriz que devuelve una FullMovie :

 http.get(movie.id).subscribe(res: FullMovie)

así que, en esencia, quiero crear un método que devuelva una secuencia de objetos FullMovie, a medida que se resuelven las solicitudes: getAll = (url): Observable<FullMovie>

 getAll = (url): Observable<FullMovie> => { return http.get(url) //must pipe the array into a stream of FullMovies but not a stream of FullMovie Observables. I don't want to subscribe to each of the returned FullMovies //something like .pipe(//map(array => array.forEach(movie => return http.get(movie.id)))) }

Por el momento tengo la siguiente solución que funciona pero quiero una solución más concisa:

 private getFull = (queryGroup: string): Observable<TMDBMovie> => new Observable<TMDBMovie>((observer) => { //get movie array this.httpGet(queryGroup).subscribe((movies) => { var j = 0; if (movies.length === 0) return observer.complete(); //loop through elements movies.forEach(movie => { this.getById(movie.id).subscribe( (res) => complete(observer.next(res)), (error) => complete() ); }); } const complete = (arg: any = 0) => { if (++j === len) observer.complete(); }; }); });

EDITAR:

Esto funciona

 newGetFull = (queryGroup: string) => this.httpGet(queryGroup) .pipe(concatMap((arr) => from(arr))) .pipe( mergeMap((movie) => this.getById(movie.id).pipe(catchError(() => of()))) );
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Es posible que desee probar algo en este sentido

 getAll = (url): Observable<FullMovie> => { return http.get(url) .pipe( // turn the array Movie[] into a stream of Movie, ie an Obsevable<Movie> concatMap(arrayOfMovies => from(arrayOfMovies)), // then use mergeMap to "flatten" the various Obaservable<FullMovie> that you get calling http.get(movie.id) // in other words, with mergeMap, you turn a stream of Observables into a stream of the results returned when each Observable is resolved mergeMap(movie => http.get(movie.id)) ) }

Considere que al usar mergeMap como se indicó anteriormente, no tiene garantía de que la transmisión final tendrá el mismo orden que la matriz de Movie que obtiene de la primera llamada. Esto se debe a que cada http.get(movie.id) puede tardar un tiempo diferente en volver y, por lo tanto, no se garantiza el pedido.

Si necesita garantizar el orden, use concatMap en lugar de mergeMap (en realidad concatMap es mergeMap con simultaneidad establecida en 1).

Si desea que se complete todo el http.get(movie.id) antes de devolver el resultado, use forkJoin en lugar de mergeMap como este

 getAll = (url): Observable<FullMovie> => { return http.get(url) .pipe( // turn the array Movie[] into an array of Observable<Movie> map(arrayOfMovies => arrayOfMovies.map(movie => http.get(movie.id))), // then use forkJoin to resolve all the Observables in parallel concatMap(arrayOfObservables => forkJoin(arrayOfObservables)) ).subscribe( arrayOfFullMovies => { // the result notified by forkJoin is an array of FullMovie objects } ) }
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!