diff --git a/q/Q-tests.ts b/q/Q-tests.ts index 6bdc5ff088..af8aa7ef23 100644 --- a/q/Q-tests.ts +++ b/q/Q-tests.ts @@ -6,15 +6,18 @@ import q = module('q'); Q(8).then(x => console.log(x.toExponential())); var delay = function (delay: number) { - var d = Q.defer(); + var d = Q.defer(); setTimeout(d.resolve, delay); return d.promise; }; -Q.when(delay(1000), function () { +Q.when(delay(1000), function (val: void) { console.log('Hello, World!'); + return; }); +Q.timeout(Q(new Date()), 1000, "My dates never arrived. :(").then(d => d.toJSON()); + Q.delay(Q(8), 1000).then(x => x.toExponential()); Q.delay(8, 1000).then(x => x.toExponential()); Q.delay(Q("asdf"), 1000).then(x => x.length); @@ -73,4 +76,14 @@ Q(arrayPromise) // type specification required declare var jPromise: JQueryPromise; // if jQuery promises definition supported generics, this could be more interesting example -Q(jPromise).then((val) => val.toExponential()); \ No newline at end of file +Q(jPromise).then((val) => val.toExponential()); + +declare var promiseArray: Q.IPromise[]; +var qPromiseArray = promiseArray.map(p => Q(p)); +var myNums: any[] = [2, 3, Q(4), 5, Q(6), Q(7)]; + +Q.all(promiseArray).then(nums => nums.map(num => num.toPrecision(2)).join(',')); + +Q.all(myNums).then(nums => nums.map(Math.round)); + +Q.fbind((dateString) => new Date(dateString), "11/11/1991")().then(d => d.toLocaleDateString()); \ No newline at end of file diff --git a/q/Q.d.ts b/q/Q.d.ts index cadca16597..3665260308 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -16,71 +16,127 @@ declare module Q { interface Deferred { promise: Promise; - resolve(value: T): any; - reject(reason: any); - notify(value: any); + resolve(value: T): void; + reject(reason: any): void; + notify(value: any): void; makeNodeResolver(): (reason, value: T) => void; } interface Promise { - fail(errorCallback: Function): Promise; - fin(finallyCallback: Function): Promise; - finally(finallyCallback: Function): Promise; + fin(finallyCallback: () => any): Promise; + finally(finallyCallback: () => any): Promise; then(onFulfill: (value: T) => U, onReject?: (reason) => U, onProgress?: Function): Promise; then(onFulfill: (value: T) => IPromise, onReject?: (reason) => U, onProgress?: Function): Promise; then(onFulfill: (value: T) => U, onReject?: (reason) => IPromise, onProgress?: Function): Promise; then(onFulfill: (value: T) => IPromise, onReject?: (reason) => IPromise, onProgress?: Function): Promise; - spread(onFulfilled: Function, onRejected?: Function): Promise; - catch(onRejected: Function): Promise; - progress(onProgress: Function): Promise; - done(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): void; - get(propertyName: String): Promise; - set(propertyName: String, value: any): Promise; - delete(propertyName: String): Promise; - post(methodName: String, args: any[]): Promise; - invoke(methodName: String, ...args: any[]): Promise; + spread(onFulfilled: Function, onRejected?: Function): Promise; + + fail(onRejected: (reason) => U): Promise; + fail(onRejected: (reason) => IPromise): Promise; + catch(onRejected: (reason) => U): Promise; + catch(onRejected: (reason) => IPromise): Promise; + + progress(onProgress: (progress) => any): Promise; + + done(onFulfilled?: (value: T) => any, onRejected?: (reason) => any, onProgress?: (progress) => any): void; + + get(propertyName: String): Promise; + set(propertyName: String, value: any): Promise; + delete(propertyName: String): Promise; + post(methodName: String, args: any[]): Promise; + invoke(methodName: String, ...args: any[]): Promise; + fapply(args: any[]): Promise; + fcall(...args: any[]): Promise; + keys(): Promise; - fapply(args: any[]): Promise; - fcall(method: Function, ...args: any[]): Promise; - timeout(ms: number, message?): Promise; + + timeout(ms: number, message?: string): Promise; delay(ms: number): Promise; + isFulfilled(): boolean; isRejected(): boolean; isPending(): boolean; + valueOf(): any; } - export function when(value: any, onFulfilled: Function, onRejected?: Function): Promise; + export function when(value: T): Promise; + export function when(value: IPromise): Promise; + export function when(value: T, onFulfilled: (val: T) => U): Promise; + export function when(value: T, onFulfilled: (val: T) => IPromise): Promise; + export function when(value: IPromise, onFulfilled: (val: T) => U, onRejected?: (reason) => U): Promise; + export function when(value: IPromise, onFulfilled: (val: T) => IPromise, onRejected?: (reason) => U): Promise; + export function when(value: IPromise, onFulfilled: (val: T) => U, onRejected?: (reason) => IPromise): Promise; + export function when(value: IPromise, onFulfilled: (val: T) => IPromise, onRejected?: (reason) => IPromise): Promise; + //export function try(method: Function, ...args: any[]): Promise; // <- This is broken currently - not sure how to fix. - export function fbind(method: Function, ...args: any[]): Promise; - export function fcall(method: Function, ...args: any[]): Promise; - export function nfbind(nodeFunction: Function): (...args: any[]) => Promise; - export function nfcall(nodeFunction: Function, ...args: any[]): Promise; - export function ninvoke(nodeModule: any, functionName: string, ...args: any[]): Promise; - export function all(promises: Promise[]): Promise; - export function allResolved(promises: Promise[]): Promise; - export function spread(onFulfilled: Function, onRejected: Function): Promise; - export function timeout(promise: Promise, ms: number, message?): Promise; + + export function fbind(method: (...args: any[]) => T, ...args: any[]): (...args: any[]) => Promise; + export function fbind(method: (...args: any[]) => IPromise, ...args: any[]): (...args: any[]) => Promise; + + export function fcall(method: (...args) => T, ...args: any[]): Promise; + + export function send(obj: any, functionName: string, ...args: any[]): Promise; + export function invoke(obj: any, functionName: string, ...args: any[]): Promise; + export function mcall(obj: any, functionName: string, ...args: any[]): Promise; + + export function nfbind(nodeFunction: Function, ...args: any[]): (...args: any[]) => Promise; + export function nfcall(nodeFunction: Function, ...args: any[]): Promise; + + export function ninvoke(nodeModule: any, functionName: string, ...args: any[]): Promise; + export function nsend(nodeModule: any, functionName: string, ...args: any[]): Promise; + export function nmcall(nodeModule: any, functionName: string, ...args: any[]): Promise; + + export function all(promises: any[]): Promise; + export function all(promises: IPromise[]): Promise; + + export function allSettled(promises: any[]): Promise[]>; + export function allSettled(promises: IPromise[]): Promise[]>; + + export function allResolved(promises: any[]): Promise[]>; + export function allResolved(promises: IPromise[]): Promise[]>; + + export function spread(promises: any[], onFulfilled: (...args: any[]) => U, onRejected: (reason) => U): Promise; + export function spread(promises: any[], onFulfilled: (...args: any[]) => IPromise, onRejected: (reason) => U): Promise; + export function spread(promises: any[], onFulfilled: (...args: any[]) => U, onRejected: (reason) => IPromise): Promise; + export function spread(promises: any[], onFulfilled: (...args: any[]) => IPromise, onRejected: (reason) => IPromise): Promise; + export function spread(promises: IPromise[], onFulfilled: (...args: T[]) => U, onRejected: (reason) => U): Promise; + export function spread(promises: IPromise[], onFulfilled: (...args: T[]) => IPromise, onRejected: (reason) => U): Promise; + export function spread(promises: IPromise[], onFulfilled: (...args: T[]) => U, onRejected: (reason) => IPromise): Promise; + export function spread(promises: IPromise[], onFulfilled: (...args: T[]) => IPromise, onRejected: (reason) => IPromise): Promise; + + export function timeout(promise: Promise, ms: number, message?: string): Promise; + export function delay(promise: Promise, ms: number): Promise; export function delay(value: T, ms: number): Promise; + export function isFulfilled(promise: Promise): boolean; export function isRejected(promise: Promise): boolean; export function isPending(promise: Promise): boolean; - export function valueOf(promise: Promise): T; + export function defer(): Deferred; + export function reject(reason?): Promise; - export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Promise; + + export function promise(resolver: (resolve: (val: T) => void , reject: (reason) => void , notify: (progress) => void ) => void ): Promise; + export function promise(resolver: (resolve: (val: IPromise) => void , reject: (reason) => void , notify: (progress) => void ) => void ): Promise; + export function promised(callback: (...any) => T): (...any) => Promise; + export function isPromise(object): boolean; export function isPromiseAlike(object): boolean; export function isPending(object): boolean; + export function async(generatorFunction: any): (...args) => Promise; export function nextTick(callback: Function): void; + export var oneerror: () => void; export var longStackSupport: boolean; - export function resolve(object): Promise; + + export function resolve(object: T): Promise; + export function resolve(object: IPromise): Promise; } declare module "q" {