Skip to content

Instantly share code, notes, and snippets.

@davidchase
Last active April 6, 2016 15:31
Show Gist options
  • Save davidchase/86168abd32c3f38456c1c03ab8160e4c to your computer and use it in GitHub Desktop.
Save davidchase/86168abd32c3f38456c1c03ab8160e4c to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<button class="btn">Click Me</button>
<script src="index.js"></script>
</body>
</html>
import {click} from '@most/dom-event';
import {fromEvent, fromPromise, combine, map} from 'most';
const cachePromise = function(triggerStream, promiseFn, n = 1) {
const signal = triggerStream.multicast()
const promiseSink = map(promiseFn, signal).await().take(n);
return combine((events, resp) => resp, signal, promiseSink);
};
const promiseF = () => fetch('http://reqres.in/api/users?page=2').then(r => r.json());
const btn = click(document.body)
.filter(event => event.target.matches('.btn'))
cachePromise(btn, promiseF).observe(console.log.bind(console));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment