Skip to content

Instantly share code, notes, and snippets.

@awto
Created May 28, 2016 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awto/d71bc466884dc9a9a6a93026ce363d17 to your computer and use it in GitHub Desktop.
Save awto/d71bc466884dc9a9a6a93026ce363d17 to your computer and use it in GitHub Desktop.
rx with mfjs for do notation
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