Created
February 12, 2022 09:24
-
-
Save nagyadam2092/bb72e894058680fc591b1c1eadf425d4 to your computer and use it in GitHub Desktop.
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
export function cancellable<A>( | |
p: Promise<A>, | |
ok: (a: A) => void, | |
err: (e: Error) => void, | |
fin?: () => void | |
) { | |
let cancelled = false; | |
p.then((v) => cancelled || ok(v)) | |
.catch((e) => cancelled || err(e)) | |
.finally(() => cancelled || fin?.()); | |
return () => { | |
cancelled = true; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment