Last active
August 5, 2019 20:42
-
-
Save branneman/6cef1a5674eb6cef261b164ffd3f3628 to your computer and use it in GitHub Desktop.
Declarative asynchonous control flow: promises, observables, futures
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
const wrapPromise = fn => (...args) => | |
new Promise(resolve => setTimeout( | |
() => resolve(fn(...args)), | |
Math.floor(Math.random() * 1500) + 250) | |
) | |
const upper = wrapPromise(str => str.toUpperCase()) | |
const underline = str => `__${str}__` | |
const exclamate = wrapPromise(str => str + '!') | |
// Util: Create promise chain | |
const chainPromises = (...fns) => val => | |
fns.reduce((ch, fn) => ch.then(fn), Promise.resolve(val)) | |
// Create function chain with mixed sync and async functions | |
const chain1 = chainPromises(upper, underline, exclamate, console.log) | |
;['aap', 'noot', 'mies'].forEach(chain1) |
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
const Observable = require('zen-observable') | |
const wrapPromise = fn => (...args) => | |
new Promise(resolve => setTimeout( | |
() => resolve(fn(...args)), | |
Math.floor(Math.random() * 1500) + 250) | |
) | |
const upper = wrapPromise(str => str.toUpperCase()) | |
const underline = str => `__${str}__` | |
const exclamate = wrapPromise(str => str + '!') | |
// Util: Promise to Observable | |
const wrapObservable = fn => (...args) => | |
new Observable(observer => { | |
fn(...args).then(val => observer.next(val)) | |
}) | |
// Create function chain with mixed sync and async functions | |
const chain2 = Observable.from(['wim', 'zus', 'jet']) | |
.flatMap(wrapObservable(upper)) | |
.map(underline) | |
.flatMap(wrapObservable(exclamate)) | |
chain2.subscribe(console.log) |
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
const { map, chain, pipe } = require('ramda') | |
const { Future, fork } = require('fluture') | |
const wrapFuture = fn => (...args) => | |
Future((_reject, resolve) => { | |
setTimeout( | |
() => resolve(fn(...args)), | |
Math.floor(Math.random() * 1500) + 250 | |
) | |
}) | |
const upper = wrapFuture(str => str.toUpperCase()) | |
const underline = str => `__${str}__` | |
const exclamate = wrapFuture(str => str + '!') | |
// Create function chain with mixed sync and async functions | |
const chain3 = pipe( | |
Future.resolve, | |
chain(upper), | |
map(underline), | |
chain(exclamate), | |
fork(console.error)(console.log) | |
) | |
map(chain3)(['teun', 'vuur', 'gijs']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment