Skip to content

Instantly share code, notes, and snippets.

@nagyadam2092
Created February 12, 2022 09:24
Show Gist options
  • Save nagyadam2092/bb72e894058680fc591b1c1eadf425d4 to your computer and use it in GitHub Desktop.
Save nagyadam2092/bb72e894058680fc591b1c1eadf425d4 to your computer and use it in GitHub Desktop.
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