Perbarui Mei 2019 menggunakan RxJs v6
Menemukan jawaban lain yang berguna, dan ingin menawarkan contoh untuk jawaban yang ditawarkan oleh Arnaud tentang zip penggunaan.
Berikut ini cuplikan yang menunjukkan kesetaraan antara Promise.alldan rxjs zip(perhatikan juga, di rxjs6 bagaimana zip sekarang diimpor menggunakan "rxjs" & bukan sebagai operator).
import { zip } from "rxjs";
const the_weather = new Promise(resolve => {
setTimeout(() => {
resolve({ temp: 29, conditions: "Sunny with Clouds" });
}, 2000);
});
const the_tweets = new Promise(resolve => {
setTimeout(() => {
resolve(["I like cake", "BBQ is good too!"]);
}, 500);
});
let source$ = zip(the_weather, the_tweets);
source$.subscribe(([weatherInfo, tweetInfo]) =>
console.log(weatherInfo, tweetInfo)
);
Promise.all([the_weather, the_tweets]).then(responses => {
const [weatherInfo, tweetInfo] = responses;
console.log(weatherInfo, tweetInfo);
});
Output dari keduanya sama. Menjalankan di atas memberikan:
{ temp: 29, conditions: 'Sunny with Clouds' } [ 'I like cake', 'BBQ is good too!' ]
{ temp: 29, conditions: 'Sunny with Clouds' } [ 'I like cake', 'BBQ is good too!' ]