From 8535df8a20c4a1b8c084b1d9cb903c3b246ab5a3 Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Wed, 17 Feb 2021 13:30:23 +0100 Subject: [PATCH] feat(utils): adds isCancelled --- src/index.ts | 1 + src/utils/index.ts | 1 + src/utils/is-cancelled.ts | 13 +++++++++++++ 3 files changed, 15 insertions(+) create mode 100644 src/utils/index.ts create mode 100644 src/utils/is-cancelled.ts diff --git a/src/index.ts b/src/index.ts index 9b9d6c8..05d1708 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export * from './definitions'; +export * from './utils'; diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..6f11fb1 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1 @@ +export * from './is-cancelled'; diff --git a/src/utils/is-cancelled.ts b/src/utils/is-cancelled.ts new file mode 100644 index 0000000..a5a9c87 --- /dev/null +++ b/src/utils/is-cancelled.ts @@ -0,0 +1,13 @@ +import { Context } from '../definitions'; + +/** + * A promise returning function that will + * resolve with `true` if `context.cancellation` + * has finalized, and `false` otherwise. + */ +export async function isCancelled(context: Context): Promise { + return new Promise((resolve) => { + context.cancellation.finally(() => resolve(true)); + setTimeout(() => resolve(false), 0); + }); +}