Created
September 25, 2018 20:48
-
-
Save Ks89/547d2b891e7aa87368ce1a7a3ee01ed4 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/misezaw
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://unpkg.com/@reactivex/[email protected]/dist/global/Rx.js"></script> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// Author Stefano Cappa (https://github.com/Ks89) | |
let letter$ = Rx.Observable.of('a'); | |
let result$ = letter$ | |
.mergeMap(response => { | |
console.log('main 1', response); | |
return doSomethingWithLetter(response); | |
}) | |
.mergeMap(response => { | |
console.log('main 2', response); | |
return doSomethingWithNum(response[1]); | |
}) | |
.mergeMap(response => { | |
console.log('main 3', response); | |
return doSomethingWithChar(response[1]); | |
}); | |
result$.subscribe( | |
x => console.log('end response', x), | |
err => console.log(err), | |
() => console.log('Done') | |
); | |
// return the result of an async obs + the input param as an array of result | |
function doSomethingWithLetter(letter) { | |
console.log('use letter here', letter); | |
let syncObs$ = Rx.Observable.of(letter); | |
let asyncObs$; | |
if (letter === 'a') { | |
console.log('it is a latter'); | |
asyncObs$ = Rx.Observable.of(letter + 1).delay(1000).combineLatest(anotherAsyncCall(letter), (a,b)=>[a,b]); | |
} else { | |
asyncObs$ = Rx.Observable.of('notA' + 1).delay(1000); | |
} | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
function anotherAsyncCall(letter) { | |
console.log('use another async call here', letter); | |
let syncObs$ = Rx.Observable.of(letter); | |
let asyncObs$ = Rx.Observable.of(letter + 'Y').delay(1000); | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
// return the result of an async obs + the input param as an array of result | |
function doSomethingWithNum(num) { | |
console.log('use num here', num); | |
let syncObs$ = Rx.Observable.of(num); | |
let asyncObs$ = Rx.Observable.of(num + '_').delay(1000); | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
// return the result of an async obs + the input param as an array of result | |
function doSomethingWithChar(char) { | |
console.log('use special char here', char); | |
let syncObs$ = Rx.Observable.of(char); | |
let asyncObs$ = Rx.Observable.of(char + 'qqqq').delay(1000); | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Author Stefano Cappa (https://github.com/Ks89) | |
let letter$ = Rx.Observable.of('a'); | |
let result$ = letter$ | |
.mergeMap(response => { | |
console.log('main 1', response); | |
return doSomethingWithLetter(response); | |
}) | |
.mergeMap(response => { | |
console.log('main 2', response); | |
return doSomethingWithNum(response[1]); | |
}) | |
.mergeMap(response => { | |
console.log('main 3', response); | |
return doSomethingWithChar(response[1]); | |
}); | |
result$.subscribe( | |
x => console.log('end response', x), | |
err => console.log(err), | |
() => console.log('Done') | |
); | |
// return the result of an async obs + the input param as an array of result | |
function doSomethingWithLetter(letter) { | |
console.log('use letter here', letter); | |
let syncObs$ = Rx.Observable.of(letter); | |
let asyncObs$; | |
if (letter === 'a') { | |
console.log('it is a latter'); | |
asyncObs$ = Rx.Observable.of(letter + 1).delay(1000).combineLatest(anotherAsyncCall(letter), (a,b)=>[a,b]); | |
} else { | |
asyncObs$ = Rx.Observable.of('notA' + 1).delay(1000); | |
} | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
function anotherAsyncCall(letter) { | |
console.log('use another async call here', letter); | |
let syncObs$ = Rx.Observable.of(letter); | |
let asyncObs$ = Rx.Observable.of(letter + 'Y').delay(1000); | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
// return the result of an async obs + the input param as an array of result | |
function doSomethingWithNum(num) { | |
console.log('use num here', num); | |
let syncObs$ = Rx.Observable.of(num); | |
let asyncObs$ = Rx.Observable.of(num + '_').delay(1000); | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
// return the result of an async obs + the input param as an array of result | |
function doSomethingWithChar(char) { | |
console.log('use special char here', char); | |
let syncObs$ = Rx.Observable.of(char); | |
let asyncObs$ = Rx.Observable.of(char + 'qqqq').delay(1000); | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
}</script></body> | |
</html> |
This file contains 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
// Author Stefano Cappa (https://github.com/Ks89) | |
let letter$ = Rx.Observable.of('a'); | |
let result$ = letter$ | |
.mergeMap(response => { | |
console.log('main 1', response); | |
return doSomethingWithLetter(response); | |
}) | |
.mergeMap(response => { | |
console.log('main 2', response); | |
return doSomethingWithNum(response[1]); | |
}) | |
.mergeMap(response => { | |
console.log('main 3', response); | |
return doSomethingWithChar(response[1]); | |
}); | |
result$.subscribe( | |
x => console.log('end response', x), | |
err => console.log(err), | |
() => console.log('Done') | |
); | |
// return the result of an async obs + the input param as an array of result | |
function doSomethingWithLetter(letter) { | |
console.log('use letter here', letter); | |
let syncObs$ = Rx.Observable.of(letter); | |
let asyncObs$; | |
if (letter === 'a') { | |
console.log('it is a latter'); | |
asyncObs$ = Rx.Observable.of(letter + 1).delay(1000).combineLatest(anotherAsyncCall(letter), (a,b)=>[a,b]); | |
} else { | |
asyncObs$ = Rx.Observable.of('notA' + 1).delay(1000); | |
} | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
function anotherAsyncCall(letter) { | |
console.log('use another async call here', letter); | |
let syncObs$ = Rx.Observable.of(letter); | |
let asyncObs$ = Rx.Observable.of(letter + 'Y').delay(1000); | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
// return the result of an async obs + the input param as an array of result | |
function doSomethingWithNum(num) { | |
console.log('use num here', num); | |
let syncObs$ = Rx.Observable.of(num); | |
let asyncObs$ = Rx.Observable.of(num + '_').delay(1000); | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} | |
// return the result of an async obs + the input param as an array of result | |
function doSomethingWithChar(char) { | |
console.log('use special char here', char); | |
let syncObs$ = Rx.Observable.of(char); | |
let asyncObs$ = Rx.Observable.of(char + 'qqqq').delay(1000); | |
return syncObs$.combineLatest(asyncObs$, (a,b) => [a, b]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment