You are on page 1of 30

combineLatest

forkJoin
withLatestFrom
combineLatest

combineLatest([a$, b$, c$])


combineLatest

combineLatest([audience$, online$, phone$])


combineLatest
combineLatest
-
-

-
-
combineLatest
forkJoin

forkJoin([a$, b$, c$])


forkJoin

forkJoin([you$, yourPartner$, yourNeighbor$])


forkJoin

forkJoin
-
-

-
-
forkJoin
withLatestFrom

a$.pipe(withLatestFrom(b$, c$))
withLatestFrom

apples$.pipe(withLatestFrom(sticks$, caramel$))
withLatestFrom
withLatestFrom
-
-

-
-
withLatestFrom
export interface Product { export interface Product {
id: number; id: number;
productName: string; productName: string;
productCode?: string; productCode?: string;
description?: string; description?: string;
price?: number; price?: number;
categoryId?: number; categoryId?: number;
} category?: string;
}
products$=this.http.get<Product[]>(this.url)
.pipe(
map(products =>
products.map(product => ({
...product,
price: product.price * 1.5,
category: ???
}) as Product)
));

category: this.getCategory(product.categoryId)

productCategories$ = this.http.get<ProductCategory[]>(this.productCategoriesUrl);
productsWithCategory$ = combineLatest([
this.products$,
this.productCategories$
]);

productsWithCategory$ = forkJoin([
this.products$,
this.productCategories$
]);

productsWithCategory$ = this.products$
.pipe(
withLatestFrom(this.productCategories$)
);
product$ = this.http.get<Product[]>(this.url);

productCategories$ = this.http.get<ProductCategory[]>(this.productCategoriesUrl);

productsWithCategory$ = combineLatest([
this.products$,
this.productCategories$
])
.pipe(
map(([products, categories]) =>
products.map(product => ({
...product,
price: product.price * 1.5,
category: categories.find(
c => product.categoryId === c.id
).name
}) as Product))
);
productsWithCategory$ = combineLatest([
this.products$,
this.productCategories$
])
.pipe(
map(([products, categories]) =>
products.map(product => ({
...product,
price: product.price * 1.5,
category: categories.find(
c => product.categoryId === c.id
).name
}) as Product))
);
combineLatest

combineLatest([a$, b$, c$])

forkJoin
forkJoin([a, b$, c$])

withLatestFrom

a$.pipe(withLatestFrom(b$, c$))
combineLatest([a$, b$, c$])
forkJoin([a$, b$, c$])
a$.pipe(withLatestFrom(b$, c$))

[a1, b1, c1]


productsWithCategory$ = combineLatest(
this.products$,
this.productCategories$
)
.pipe(
map([products, categories] =>
products.map(product => ({
...product,
category: categories.find(
c => product.categoryId === c.id
).name
}) as Product))
);

You might also like