Published
Edited
Nov 12, 2019
Insert cell
Insert cell
Insert cell
condition = () => Math.random() > 0.5 // a condition to perform the call
Insert cell
call = x => obs.of(x).delay(1000) // mimics a rest call
Insert cell
assemble = (a, b) => ({a: a, b: b}) // combines the result of both calls
Insert cell
Insert cell
obs.zip(
obs.of(condition()).flatMap(x => x ? call("service1") : obs.of(null)),
obs.of(condition()).flatMap(x => x ? call("service2") : obs.of(null)),
assemble)
.value()
Insert cell
Insert cell
Insert cell
obs.of( { predicate: condition, action: call("service1") },
{ predicate: condition, action: call("service2") } )
.map( ({ predicate, action }) => predicate() ? action : obs.of(null) )
.zipAll(assemble)
.value()
Insert cell
Insert cell
Insert cell
obs.of( { predicate: condition, action: call("service1") },
{ predicate: condition, action: call("service2") } )
.pipe(conditionalZip(assemble))
.value()
Insert cell
conditionalZip =
combine =>
stream =>
stream
.map( ({ predicate, action }) => predicate() ? action : obs.of(null) )
.zipAll(combine)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more