Skip to content

Commit

Permalink
🤖 Merge PR #45014 [callback-to-async-iterator] Fixes + allow usage of…
Browse files Browse the repository at this point in the history
… listener return value in onClose by @affanshahid

allow usage of return val from listener in onClose
  • Loading branch information
affanshahid committed May 23, 2020
1 parent 922f906 commit e4d3b1b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { callbackToAsyncIterator } from "callback-to-async-iterator";
import callbackToAsyncIterator from "callback-to-async-iterator";

callbackToAsyncIterator(() => {}); // $ExpectType AsyncIterator<unknown, any, undefined>
callbackToAsyncIterator(async () => {}); // $ExpectType AsyncIterator<unknown, any, undefined>

callbackToAsyncIterator<string>(() => {}); // $ExpectType AsyncIterator<string, any, undefined>
callbackToAsyncIterator<string>(async () => {}); // $ExpectType AsyncIterator<string, any, undefined>

callbackToAsyncIterator<string>(async () => {}, { onClose() {} }); // $ExpectType AsyncIterator<string, any, undefined>

callbackToAsyncIterator<string, number>(async () => 1, { onClose(arg: number) {} }); // $ExpectType AsyncIterator<string, any, undefined>
15 changes: 8 additions & 7 deletions types/callback-to-async-iterator/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// Type definitions for callback-to-async-iterator 1.1
// Project: https://github.com/withspectrum/callback-to-async-iterator#readme
// Definitions by: Zachary Svoboda <https://github.com/zacnomore>
// Affan Shahid <https://github.com/affanshahid>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.6

export interface AsyncifyOptions<T> {
onClose: () => void | T;
onError: () => Error;
buffering: boolean;
export interface AsyncifyOptions<T, R> {
onClose?: (arg: R) => void | T;
onError?: () => Error;
buffering?: boolean;
}

export function callbackToAsyncIterator<T>(
listener: (callback: (message: T) => void) => void,
options?: AsyncifyOptions<T>,
export default function callbackToAsyncIterator<T, R = void>(
listener: (callback: (message: T) => void) => Promise<R>,
options?: AsyncifyOptions<T, R>,
): AsyncIterator<T>;

0 comments on commit e4d3b1b

Please sign in to comment.