Skip to content

Instantly share code, notes, and snippets.

class AppComponent {
constructor () {
this.count = 5;
this.onResize = this._onResize.bind(this);
}
componentDidMount () {
window.addEventListener('resize', this.onResize)
}
export class AppComponent {
onDestroy$ = new Subject();
count = 5;
ngOnInit () {
this.iceCreams$ = fromEvent(window, 'resize').pipe(
flatMap(() => fromPromise(fetchIceCreams(this.count))),
takeUntil(this.onDestroy$)
);
}
const filterLoggedIn$ = (stream$) => {
return stream$.pipe(
withLatestFrom(iceCreamAuth$),
filter((auth) => auth)
)
};
const combinedInfoFactory$ = (
iceCreamPreferences$,
iceCreamInventory$,
from(iceCreamIds).pipe(
withLatestFrom(
iceCreamAuth$,
iceCreamPreferences$,
iceCreamVendors$,
iceCreamInventory$,
iceCreamTemperature$,
),
map(([
import { from, fromPromise } from 'rxjs';
const iceCreamIds = [4, 5, 10, 12];
from(iceCreamIds).pipe(
flatMap((iceCreamId) => fromPromise(getIceCreamById(iceCreamId)))
).subscribe((iceCream) => {
console.log(iceCream);
});
function getIceCreamById (iceCreamId) {
return fetch(`/icecream/${id}`);
}
const iceCreamIds = [4, 5, 10, 12];
// Slow, effectively synchronous
for (let iceCreamId of iceCreamIds) {
console.log(await getIceCreamById(iceCreamId));
}
import { from, fromEvent, zip } from 'rxjs';
import { scan } from 'rxjs/operators';
const initialIceCreamFlavors = ['vanilla', 'chocolate', 'strawberry'];
const initialIceCreamPrices = ['$5.50', '$5.75', '$6.00'];
const iceCreamFormSubmit$ = fromEvent(formEl, 'submit');
const iceCreamFormFlavors$ = iceCreamFormSubmit$.pipe((map(({flavor}) => flavor)));
const iceCreamFormPrices$ = iceCreamFormSubmit$.pipe((map(({price}) => price)));
function getIceCreamFlavorToPrice(flavors, prices) {
let iceCreamFlavorToPrice;
for (let i = 0; i < flavors.length; i++) {
iceCreamFlavorToPrice[flavors[i]] = prices[i];
}
return iceCreamFlavorToPrice;
}
let iceCreamFlavors = ['vanilla', 'chocolate', 'strawberry'];
let iceCreamPrices = ['$5.50', '$5.75', '$6.00'];
import { from, zip } from 'rxjs';
import { scan } from 'rxjs/operators';
const iceCreamFlavors = ['vanilla', 'chocolate', 'strawberry'];
const iceCreamPrices = ['$5.50', '$5.75', '$6.00'];
const iceCreamFlavorToPrice$ = zip(
from(iceCreamFlavors),
from(iceCreamPrices),
).pipe(scan((prev, [flavor, price]) => {
const iceCreamFlavors = ['vanilla', 'chocolate', 'strawberry'];
const iceCreamPrices = ['$5.50', '$5.75', '$6.00'];
let iceCreamFlavorToPrice;
for (let i = 0; i < iceCreamFlavors.length; i++) {
iceCreamFlavorToPrice[iceCreamFlavors[i]] = iceCreamPrices[i];
}