Created
May 28, 2016 11:12
-
-
Save awto/d71bc466884dc9a9a6a93026ce363d17 to your computer and use it in GitHub Desktop.
rx with mfjs for do notation
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
import {Observable} from 'rx' | |
const M = require('@mfjs/core') | |
const MM = require('@mfjs/rx')() | |
M.profile('regenerator') | |
const { just: pure, fromPromise,from } = Observable | |
let cnt = 0 | |
function updateDb() { | |
return new Promise(resolve => { | |
setTimeout( | |
() => { | |
console.log(`DB UPDATED ${cnt}`) | |
resolve(cnt++) | |
}, 0) | |
}) | |
} | |
MM.run(function* () { | |
const m = yield fromPromise(updateDb()) | |
const x = yield from([1,2]) | |
return `${x} ${m}` | |
}).subscribe(function(res) { | |
console.log(res) | |
}) | |
/* | |
outputs: | |
``` | |
DB UPDATED 0 | |
1 0 | |
2 0 | |
``` | |
*/ | |
let i = 0; | |
MM.run(function*() { | |
const x = yield from([1,2]) | |
if (i++) { | |
const y = yield from(['in then']) | |
return `then ${x} ${y}` | |
} else { | |
const y = yield from(['in else']) | |
return `else ${x} ${y}` | |
} | |
}).subscribe(function(res) { | |
console.log(res) | |
}) | |
/* | |
outputs: | |
``` | |
else 1 in else | |
then 2 in then | |
``` | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment