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