combineLatest<\/i> demo<\/a>:<\/p>\nconst weight = of(70, 72, 76, 79, 75);\r\nconst height = of(1.76, 1.77, 1.78);\r\n\r\nconst bmi = weight.pipe(\r\n withLatestFrom(height, (w, h) => {\r\n console.log('project values: w =', w, ', h =', h);\r\n return w \/ (h * h);\r\n}));\r\nbmi.subscribe(res => console.log('BMI is ' + res));\r\n\r\n\/\/ Output to console is:\r\nproject values: w = 70 , h = 1.78\r\nBMI is 22.093170054286073\r\nproject values: w = 72 , h = 1.78\r\nBMI is 22.724403484408533\r\nproject values: w = 76 , h = 1.78\r\nBMI is 23.98687034465345\r\nproject values: w = 79 , h = 1.78\r\nBMI is 24.933720489837143\r\nproject values: w = 75 , h = 1.78\r\nBMI is 23.671253629592222\r\n<\/pre>\nIn the above example, the weight<\/i> observable provides the source stream. As such, withLatestFrom<\/i> calculated the BMI whenever it emits a value. In each case, th final value height<\/i> value of 1.78 is utilized.<\/p>\n
Compare that output to what was produced by combineLatest<\/i>:<\/p>\n\/\/ Output to console is:\r\n\/\/ project values: w = 75, h = 1.76\r\n\/\/ BMI is 24.212293388429753\r\n\/\/ project values: w = 75, h = 1.77\r\n\/\/ BMI is 23.93948099205209\r\n\/\/ project values: w = 75, h = 1.78\r\n\/\/ BMI is 23.671253629592222\r\n<\/pre>\nIt only emitted three BMI values – one of each input stream emission combinations. In this case, the number of values emitted from combineLatest<\/i> was determined by the stream that emitted the least number of values, which was height<\/i>.<\/p>\nzip in RxJS<\/i><\/h2>\n
Typically, an operator’s name reveals enough about its functioning to remind us what it does, once you’ve familiarized yourself with it. In the case of zip<\/i>, we have an operator that combines observable streams in a way that mimics the behavior of a zipper on clothing or a bag. As such, it combines two or more stream sequences as an array of values whose length is equal to the number of input streams provided, i.e., a pair in case of two input streams. To do that, zip<\/i> waits for values to be emitted from all input streams, then emits them as an array.<\/p>\n
It is also worth noting that zip<\/i> will only emit an array once it has fresh values from each source sequence. So if one of the source observables emits values faster than the others, the rate of publishing will be dictated by the slowest stream.<\/p>\n
Zip<\/i> stops emitting values when any of the input streams complete and the corresponding new values are emitted from other streams. Conversely, it will never complete if none of the input streams completes and will throw an error if any of the inner streams errors out.<\/p>\n
Here is zip<\/i>‘s signature:<\/p>\nzip(observable_1<\/i>[, observable_2<\/i>...observable_n<\/i>]): Observable\r\n<\/pre>\n The following code shows the zip<\/i> operator in action:<\/p>\nimport { map } from 'rxjs\/operators';\r\nimport { of, zip } from 'rxjs';\r\n\r\nlet age$ = of<number>(30, 29, 44);\r\nlet name$ = of<string>('Bob', 'Carol', 'Jim');\r\nlet isDev$ = of<boolean>(true, true, false, false);\r\n\r\nzip(age$, name$, isDev$)\r\n .pipe(map(([age, name, isDev]) => ({ age, name, isDev })))\r\n .subscribe(res => console.log(res));\r\n\r\n\/\/ outputs\r\n\/\/ { age: 30, name: 'Bob', isDev: true }\r\n\/\/ { age: 29, name: 'Carol', isDev: true }\r\n\/\/ { age: 44, name: 'Jim', isDev: false }\r\n<\/pre>\nIn the above snippet, three streams that emit all of there values without any delay. The zip<\/i> output is piped to the RxJS map<\/i> operator so that input parameters are displayed as name: value<\/i> pairs in the subscribe function. Notice that the last boolean value of false<\/i> is never emitted because the other two streams have completed by that point.<\/p>\nA zip<\/i> Example with Delay<\/h3>\n
As this second example shows, introducing a delay to stream emissions does not change the results:<\/p>\n
zip(\r\n age$.pipe(delay(2000)), \r\n name$.pipe(delay(1000)), \r\n isDev$.pipe(delay(3000))\r\n)\r\n .pipe(map(([age, name, isDev]) => ({ age, name, isDev })))\r\n .subscribe(res => console.log(res));\r\n \r\n\/\/ outputs\r\n\/\/ { age: 30, name: 'Bob', isDev: true }\r\n\/\/ { age: 29, name: 'Carol', isDev: true }\r\n\/\/ { age: 44, name: 'Jim', isDev: false }\r\n<\/pre>\nThe only difference is that zip<\/i> won’t emit anything until it has received values from each of the input streams, meaning that the subscribe function won’t receive a value until the isDev$<\/i> observable emits after a delay of 3 seconds. All values are subsequently emitted in quick succession.<\/p>\nRxJS Combination Operators<\/h2>\n
In this tutorial, we learned how to use two of the RxJS library’s two most powerful combination operators: withLatestFrom<\/i> and zip<\/i>.<\/p>\n
All of the above code snippets are included in the stackblitz.com demo<\/a> so that you can play with the code and observe the results.<\/p>\n","protected":false},"excerpt":{"rendered":"In Angular applications, we often rely on more than one asynchronous call to load component data. This often necessitates that we hold off on initializing the component until we have received all of the responses. One way to do that is to employ some of the RxJS library’s plethora of combination operators. In this tutorial, […]<\/p>\n","protected":false},"author":90,"featured_media":11067,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30620],"tags":[3385,33896,30872],"b2b_audience":[37],"b2b_industry":[65],"b2b_product":[76,131,133,107,94,99,80,128,98],"acf":[],"yoast_head":"\n
RxJS Observables: withLatestFrom and zip | HTMLGoodies.com<\/title>\n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n\t \n\t \n\t \n