Skip to content

Instantly share code, notes, and snippets.

@cowboyd
cowboyd / async-function-typeclasses.js
Last active October 11, 2018 15:42
Funcadelic typeclass implementations for AsyncFunction
/**
* There's some pretty awesome low-level composition that you can do with async functions:
* Check out funcadelic! https://github.com/cowboyd/funcadelic.js
*/
import { Functor, Semigroup, Monoid } from 'funcadelic';
const AsyncFunction = (async function() {}).constructor;
/**
* It's a Functor!
@cowboyd
cowboyd / 1-click-to-load.js
Last active April 11, 2023 10:54
Handle a stream of clicks by canceling the latest one
/**
* Implements an operation to fetch the data as described in https://twitter.com/BenLesh/status/1455597411385098242
* The "winner" is unambiguous. It is the request corresponding to the latest click, guaranteed.
* Other important things to note:
*
* 1. The HTTP request is cancelled every time along with its containing task
* 2. click listener is removed automatically at the end of the `clickToLoad` operation
* 3. The cancellation logic is explicit here to demonstrate the concept, but it can easily be abstracted into a separate
*. function. See the `click-to-load-HOF` example below
*/