From 7b7c26088f356b587dd2146193d12b90ccc568a7 Mon Sep 17 00:00:00 2001 From: Okiki Date: Tue, 27 Aug 2024 00:46:55 +0000 Subject: [PATCH] feat: add Future.is(...) Signed-off-by: Okiki --- from.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/from.ts b/from.ts index f2fbdbf..0bc09d6 100644 --- a/from.ts +++ b/from.ts @@ -143,10 +143,10 @@ export function from( | Iterator, TReturn | PromiseLike, TNext> | PromiseLike | T -) { +) { // Handle Future instances directly - if (operation instanceof Future) { - return operation; + if (is(operation)) { + return operation as Future; } // Handle ReadableStreams (common in web APIs) @@ -192,7 +192,7 @@ export function fromPromise(promise: PromiseLike): Future { }); } -export function fromOperation(operation: FutureFromOperation) { +export function fromOperation(operation: FutureFromOperation): Future { return new Future(async function* (abort: AbortController) { const result = operation(abort); @@ -291,4 +291,13 @@ export function fromStream(stream: ReadableStream): Future { return undefined; }); +} + +/** + * Creates a new `Future` instance from an async generator function. + * @param value - A function that returns an async generator to define the asynchronous task. + * @returns A new `Future` instance. * + */ +export function is(value: unknown): value is Future { + return value instanceof Future; } \ No newline at end of file -- 2.51.2