Aggiornamento maggio 2019 utilizzando RxJs v6
Ho trovato utili le altre risposte e ho voluto offrire un esempio per la risposta offerta da Arnaud zipsull'uso.
Ecco uno snippet che mostra l'equivalenza tra Promise.alle rxjs zip(nota anche, in rxjs6 come zip ora viene importato usando "rxjs" e non come operatore).
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);
});
L'output di entrambi è lo stesso. L'esecuzione di quanto sopra dà:
{ 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!' ]