// Type definitions for asyncawait // Project: https://github.com/yortus/asyncawait // Definitions by: Troy Gerwien declare module AsyncAwait { //------------------------- Async ------------------------- export interface Async extends AsyncReturnsPromise { cps: AsyncAcceptsCallbackReturnsNothing; thunk: AsyncReturnsThunk; result: AsyncReturnsResult; iterable: AsyncIterableReturnsPromise; } export interface AsyncFunction { (fn: Function): Function; // These overloads provide enhanced type information to TypeScript users. The strings must match exactly. mod(options: 'returns: promise, callback: false, iterable: false' , maxConcurrency?: number): AsyncReturnsPromise; mod(options: 'returns: thunk, callback: false, iterable: false' , maxConcurrency?: number): AsyncReturnsThunk; mod(options: 'returns: result, callback: false, iterable: false' , maxConcurrency?: number): AsyncReturnsResult; mod(options: 'returns: promise, callback: true, iterable: false' , maxConcurrency?: number): AsyncAcceptsCallbackReturnsPromise; mod(options: 'returns: thunk, callback: true, iterable: false' , maxConcurrency?: number): AsyncAcceptsCallbackReturnsThunk; mod(options: 'returns: result, callback: true, iterable: false' , maxConcurrency?: number): AsyncAcceptsCallbackReturnsResult; mod(options: 'returns: none, callback: true, iterable: false' , maxConcurrency?: number): AsyncAcceptsCallbackReturnsNothing; mod(options: 'returns: promise, callback: false, iterable: true' , maxConcurrency?: number): AsyncIterableReturnsPromise; mod(options: 'returns: thunk, callback: false, iterable: true' , maxConcurrency?: number): AsyncIterableReturnsThunk; mod(options: 'returns: result, callback: false, iterable: true' , maxConcurrency?: number): AsyncIterableReturnsResult; mod(options: 'returns: promise, callback: true, iterable: true' , maxConcurrency?: number): AsyncIterableAcceptsCallbackReturnsPromise; mod(options: 'returns: thunk, callback: true, iterable: true' , maxConcurrency?: number): AsyncIterableAcceptsCallbackReturnsThunk; mod(options: 'returns: result, callback: true, iterable: true' , maxConcurrency?: number): AsyncIterableAcceptsCallbackReturnsResult; mod(options: 'returns: none, callback: true, iterable: true' , maxConcurrency?: number): AsyncIterableAcceptsCallbackReturnsNothing; mod(options: string, maxConcurrency?: number): AsyncFunction; mod(options: AsyncOptions): AsyncFunction; } export interface AsyncOptions { returnValue?: string; // Recognised values: 'none', 'promise', 'thunk', 'result' acceptsCallback?: boolean; isIterable?: boolean; maxConcurrency?: number; } export interface AsyncReturnsPromise extends AsyncFunction { (fn: () => TResult): () => Thenable; (fn: (arg: T) => TResult): (arg: T) => Thenable; (fn: (arg1: T1, arg2: T2) => TResult): (arg1: T1, arg2: T2) => Thenable; (fn: (arg1: T1, arg2: T2, arg3: T3) => TResult): (arg1: T1, arg2: T2, arg3: T3) => Thenable; (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Thenable; } export interface AsyncReturnsThunk extends AsyncFunction { (fn: () => TResult): () => Thunk; (fn: (arg: T) => TResult): (arg: T) => Thunk; (fn: (arg1: T1, arg2: T2) => TResult): (arg1: T1, arg2: T2) => Thunk; (fn: (arg1: T1, arg2: T2, arg3: T3) => TResult): (arg1: T1, arg2: T2, arg3: T3) => Thunk; (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Thunk; } export interface AsyncReturnsResult extends AsyncFunction { (fn: () => TResult): () => TResult; (fn: (arg: T) => TResult): (arg: T) => TResult; (fn: (arg1: T1, arg2: T2) => TResult): (arg1: T1, arg2: T2) => TResult; (fn: (arg1: T1, arg2: T2, arg3: T3) => TResult): (arg1: T1, arg2: T2, arg3: T3) => TResult; (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult; } export interface AsyncAcceptsCallbackReturnsPromise extends AsyncFunction { (fn: () => TResult): (callback?: Callback) => Thenable; (fn: (arg: T) => TResult): (arg: T, callback?: Callback) => Thenable; (fn: (arg1: T1, arg2: T2) => TResult): (arg1: T1, arg2: T2, callback?: Callback) => Thenable; (fn: (arg1: T1, arg2: T2, arg3: T3) => TResult): (arg1: T1, arg2: T2, arg3: T3, callback?: Callback) => Thenable; (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback?: Callback) => Thenable; } export interface AsyncAcceptsCallbackReturnsThunk extends AsyncFunction { (fn: () => TResult): (callback?: Callback) => Thunk; (fn: (arg: T) => TResult): (arg: T, callback?: Callback) => Thunk; (fn: (arg1: T1, arg2: T2) => TResult): (arg1: T1, arg2: T2, callback?: Callback) => Thunk; (fn: (arg1: T1, arg2: T2, arg3: T3) => TResult): (arg1: T1, arg2: T2, arg3: T3, callback?: Callback) => Thunk; (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback?: Callback) => Thunk; } export interface AsyncAcceptsCallbackReturnsResult extends AsyncFunction { (fn: () => TResult): (callback?: Callback) => TResult; (fn: (arg: T) => TResult): (arg: T, callback?: Callback) => TResult; (fn: (arg1: T1, arg2: T2) => TResult): (arg1: T1, arg2: T2, callback?: Callback) => TResult; (fn: (arg1: T1, arg2: T2, arg3: T3) => TResult): (arg1: T1, arg2: T2, arg3: T3, callback?: Callback) => TResult; (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback?: Callback) => TResult; } export interface AsyncAcceptsCallbackReturnsNothing extends AsyncFunction { (fn: () => TResult): (callback?: Callback) => void; (fn: (arg: T) => TResult): (arg: T, callback?: Callback) => void; (fn: (arg1: T1, arg2: T2) => TResult): (arg1: T1, arg2: T2, callback?: Callback) => void; (fn: (arg1: T1, arg2: T2, arg3: T3) => TResult): (arg1: T1, arg2: T2, arg3: T3, callback?: Callback) => void; (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback?: Callback) => void; } export interface AsyncIterableReturnsPromise extends AsyncFunction { (fn: Function): () => { next(): Thenable<{ done: boolean; value?: any; }>; forEach(callback: (value) => void): Thenable; }; } export interface AsyncIterableReturnsThunk extends AsyncFunction { (fn: Function): () => { next(): Thunk<{ done: boolean; value?: any; }>; forEach(callback: (value) => void): Thunk; }; } export interface AsyncIterableReturnsResult extends AsyncFunction { (fn: Function): () => { next(): { done: boolean; value?: any; }; forEach(callback: (value) => void): void; }; } export interface AsyncIterableAcceptsCallbackReturnsPromise extends AsyncFunction { (fn: Function): () => { next(callback?: Callback): Thenable<{ done: boolean; value?: any; }>; forEach(callback: (value) => void, doneCallback?: Callback): Thenable; }; } export interface AsyncIterableAcceptsCallbackReturnsThunk extends AsyncFunction { (fn: Function): () => { next(callback?: Callback): Thunk<{ done: boolean; value?: any; }>; forEach(callback: (value) => void, doneCallback?: Callback): Thunk; }; } export interface AsyncIterableAcceptsCallbackReturnsResult extends AsyncFunction { (fn: Function): () => { next(callback?: Callback): { done: boolean; value?: any; }; forEach(callback: (value) => void, doneCallback?: Callback): void; }; } export interface AsyncIterableAcceptsCallbackReturnsNothing extends AsyncFunction { (fn: Function): () => { next(callback?: Callback): void; forEach(callback: (value) => void, doneCallback?: Callback): void; }; } //------------------------- Await ------------------------- export interface Await extends AwaitFunction { in: AwaitFunction; top(n: number): AwaitFunction; } export interface AwaitFunction { (expr: Thenable): T; (expr: Thenable[]): T[]; (expr: Thunk): T; (expr: Thunk[]): T[]; (expr: Object): Object; } //------------------------- Common ------------------------- export interface Thenable { then(onResolved: (value: T) => Thenable, onRejected: (error: any) => Thenable): Thenable; then(onResolved: (value: T) => Thenable, onRejected?: (error: any) => U): Thenable; then(onResolved: (value: T) => U, onRejected: (error: any) => Thenable): Thenable; then(onResolved?: (value: T) => U, onRejected?: (error: any) => U): Thenable; } export interface Callback { (err: any, result: TResult): void; } export interface Thunk { (callback?: (err, result?) => void): void; } } declare module "asyncawait" { export import async = require("asyncawait/async"); export import await = require("asyncawait/await"); } declare module "asyncawait/async" { /** * Creates a suspendable function. Suspendable functions may use the await() function * internally to suspend execution at arbitrary points, pending the results of * internal asynchronous operations. * @param {Function} fn - Contains the body of the suspendable function. Calls to await() * may appear inside this function. * @returns {Function} A function of the form `(...args) --> Promise`. Any arguments * passed to this function are passed through to fn. The returned * promise is resolved when fn returns, or rejected if fn throws. */ var M: AsyncAwait.Async; export = M; } declare module "asyncawait/await" { /** * Suspends a suspendable function until the given awaitable expression produces * a result. If the given expression produces an error, then an exception is raised * in the suspendable function. * @param {any} expr - The awaitable expression whose results are to be awaited. * @returns {any} The final result of the given awaitable expression. */ var M: AsyncAwait.Await export = M; }