fafgag
v0.3.4
Published
Creates an Observable from FAFGAG (Function / Async Function / Generator / Async Generator)
Downloads
10
Maintainers
Readme
FAFGAG
Creates an Observable from FAFGAG (Function / Async Function / Generator / Async Generator)
Installation
$ npm install fafgag
Usage
fafgag
converts the results which returned from FAFGAG into Observables:
- Function --> Value --> Observable
- Async Function --> Promise --> Observable
- Generator --> Iterator --> Observable
- Async Generator --> Async Iterator --> Observable
import toObservable from "fafgag"
function f(n) {
return n * n
}
async function af(n) {
await sleep(1000)
return n * n
}
function* g(n) {
yield n * n
yield (n + 1) * (n + 1)
}
await function* ag(n) {
yield n * n
await sleep(1000)
yield (n + 1) * (n + 1)
}
toObservable(f, 5).subscribe(console.log) // output: 25
toObservable(af, 5).subscribe(console.log) // output: (one sec later) 25
toObservable(g, 5).subscribe(console.log) // output: 25 36
toObservable(ag, 5).subscribe(console.log) // output: 26 (one sec later) 36
Note: only when it get subscribed, it will run. This laziness is the basic behavior of Observables.
Native / Zen
If you prefer Babel's polyfill, import fafgag
like this:
import toObservable from "fafgag"
If you prefer zen-observable, import fafgag
like this:
import toObservable from "fafgag/lib/zen.js" // for ES module environment
import toObservable from "fafgag/zen" // for other environments
| | UMD | ES module | Note |
| :-- | :-- | :-- | :-- |
| native (babel) | "fafgag" | "fafgag" | standard, but w/o these methods below |
| zen-observable | "fafgag/zen" | "fafgag/lib/zen.js" | map()
, filter()
, flatMap()
, etc. |
License
MIT