Skip to content

Instantly share code, notes, and snippets.

@caasi
Created October 4, 2018 15:00
Show Gist options
  • Save caasi/c16b9c573c993bbc08bc7b3acb507282 to your computer and use it in GitHub Desktop.
Save caasi/c16b9c573c993bbc08bc7b3acb507282 to your computer and use it in GitHub Desktop.
Race two callbacks
type Cont = (callback: (a: any) => void) => void;
const timeout500: Cont = (next) => setTimeout(next, 500, 'timeout500');
const timeout1000: Cont = (next) => setTimeout(next, 1000, 'timeout1000');
const race
: (a: Cont, b: Cont) => Cont
= (a, b) => (next) => {
let resolved = false;
const f = (a) => {
if (!resolved) {
resolved = true;
next(a);
}
}
a(f);
b(f);
}
race(timeout500, timeout1000)(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment