Created
October 4, 2018 15:00
-
-
Save caasi/c16b9c573c993bbc08bc7b3acb507282 to your computer and use it in GitHub Desktop.
Race two callbacks
This file contains hidden or 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
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