made with esnextbin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Observable = Rx.Observable; | |
// get a stream of keypresses (or even keyups) | |
var keypresses = Observable.fromEvent(myinput, 'keypress'); | |
var subscription; | |
// throttled to every 500ms | |
subscription = keypresses.throttle(500). | |
// to a stream of values | |
map(function() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Running this file in Node (>4) will demonstrate that unhandled exceptions in promises will | |
* cause the process to continue running in a happy way, logging all 3 numbers. | |
* */ | |
setTimeout(function () { | |
console.log(3); | |
}, 100); | |
console.log(1); | |
new Promise(function () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
const path = ["list", 0]; | |
const path0 = ["list", 20, 'name']; // ref ref | |
const path1 = ["list", [0, 1], "name"]; | |
const path2 = ["list", [0, 1, { from: 3, length: 2 }], "name"]; | |
const path3 = ["list", [0, 1, { from: 3, length: 2 }, { from: 7, to: 9 }], "name"]; | |
const path4 = ["list", { to: 5 }, ["name", "rating"]]; | |
const jsonGraph = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An observable that emits 10 multiples of 100 every 1 second | |
const source$ = Observable.interval(1000) | |
.take(10) | |
.map(x => x * 100); | |
/** | |
* returns a promise that waits `ms` milliseconds and emits "done" | |
*/ | |
function promiseDelay(ms) { | |
return new Promise(resolve => { | |
setTimeout(() => resolve('done'), ms); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An observable that emits 10 multiples of 100 every 1 second | |
const source$ = Observable.interval(1000) | |
.take(10) | |
.map(x => x * 100); | |
/** | |
* returns a promise that waits `ms` milliseconds and emits "done" | |
*/ | |
function promiseDelay(ms) { | |
return new Promise(resolve => { | |
setTimeout(() => resolve('done'), ms); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getErroringPromise() { | |
console.log('getErroringPromise called'); | |
return Promise.reject(new Error('sad')); | |
} | |
Observable.defer(getErroringPromise) | |
.retry(3) | |
.subscribe(x => console.log); | |
// logs "getErroringPromise called" 4 times (once + 3 retries), then errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Observable.defer(async function() { | |
const a = await promiseDelay(1000).then(() => 1); | |
const b = a + await promiseDelay(1000).then(() => 2); | |
return a + b + await promiseDelay(1000).then(() => 3); | |
}) | |
.subscribe(x => console.log(x)) // logs 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const click$ = Observable.fromEvent(button, 'clicks'); | |
/** | |
* Waits for 10 clicks of the button | |
* then posts a timestamp of the tenth click to an endpoint | |
* using fetch | |
*/ | |
async function doWork() { | |
await click$.take(10) | |
.forEach((_, i) => console.log(`click ${i + 1}`)); | |
return await fetch( |