From a615315e63d0482e6ab0cfe443064f14d69cf260 Mon Sep 17 00:00:00 2001 From: Andrew Gaspar Date: Wed, 19 Jun 2013 23:18:45 -0700 Subject: [PATCH 1/7] Merged q.d.ts and q.module.d.ts using new semantics for export = . Hoozah! --- q/Q-tests.ts | 2 ++ q/Q.d.ts | 58 +++++++++++++++++++++++++-------------------- q/q.module-tests.ts | 32 +++++++++++++------------ q/q.module.d.ts | 31 ------------------------ 4 files changed, 51 insertions(+), 72 deletions(-) delete mode 100644 q/q.module.d.ts diff --git a/q/Q-tests.ts b/q/Q-tests.ts index 34cde9341..65a19d008 100644 --- a/q/Q-tests.ts +++ b/q/Q-tests.ts @@ -1,5 +1,7 @@ /// +Q(8).then(x => console.log(x)); + var delay = function (delay) { var d = Q.defer(); setTimeout(d.resolve, delay); diff --git a/q/Q.d.ts b/q/Q.d.ts index 15f3fdea7..f1835241d 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -35,30 +35,36 @@ interface Qpromise { valueOf(): any; } -interface QStatic { - when(value: any, onFulfilled?: Function, onRejected?: Function): Qpromise; - try(method: Function, ...args: any[]): Qpromise; - fbind(method: Function, ...args: any[]): Qpromise; - fcall(method: Function, ...args: any[]): Qpromise; - all(promises: Qpromise[]): Qpromise; - allResolved(promises: Qpromise[]): Qpromise; - resolve(object:any):Qpromise; - spread(onFulfilled: Function, onRejected: Function): Qpromise; - timeout(ms: number): Qpromise; - delay(ms: number): Qpromise; - delay(value: any, ms: number): Qpromise; - isFulfilled(): bool; - isRejected(): bool; - isPending(): bool; - valueOf(): any; - defer(): Qdeferred; - (value: any): Qpromise; - reject(): Qpromise; - promise(factory: { resolve: Function; reject: Function; notify: Function; }): Qpromise; - isPromise(value: any): bool; - async(generatorFunction: any): Qdeferred; - nextTick(callback: Function); - oneerror: any; - longStackJumpLimit: number; +declare function Q(value: any): Qpromise; +declare module Q { + export function when(value: any, onFulfilled: Function, onRejected?: Function): Qpromise; + //export function try(method: Function, ...args: any[]): Qpromise; <- This is broken currently - not sure how to fix. + export function fbind(method: Function, ...args: any[]): Qpromise; + export function fcall(method: Function, ...args: any[]): Qpromise; + export function nfbind(nodeFunction: Function): (...args: any[]) => Qpromise; + export function nfcall(nodeFunction: Function, ...args: any[]): Qpromise; + export function ninvoke(nodeModule: any, functionName: string, ...args: any[]): Qpromise; + export function all(promises: Qpromise[]): Qpromise; + export function allResolved(promises: Qpromise[]): Qpromise; + export function spread(onFulfilled: Function, onRejected: Function): Qpromise; + export function timeout(ms: number): Qpromise; + export function delay(ms: number): Qpromise; + export function delay(value: any, ms: number): Qpromise; + export function isFulfilled(): bool; + export function isRejected(): bool; + export function isPending(): bool; + export function valueOf(): any; + export function defer(): Qdeferred; + export function reject(): Qpromise; + export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Qpromise; + export function isPromise(value: any): bool; + export function async(generatorFunction: any): Qdeferred; + export function nextTick(callback: Function); + export var oneerror: any; + export var longStackJumpLimit: number; + export function resolve(object?: Qpromise); } -declare var Q: QStatic; + +declare module "q" { + export = Q; +} \ No newline at end of file diff --git a/q/q.module-tests.ts b/q/q.module-tests.ts index 4f9039d23..2d32febca 100644 --- a/q/q.module-tests.ts +++ b/q/q.module-tests.ts @@ -1,30 +1,32 @@ -/// +/// /// /// -import Q = module("q"); +import q = module("q"); import fs = module("fs"); +q(8).then(x => console.log(x)); + var delay = function (delay) { - 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 () { console.log('Hello, World!'); }); var eventually = function (eventually) { - return Q.delay(eventually, 1000); + return q.delay(eventually, 1000); }; -var x = Q.all([1, 2, 3].map(eventually)); -Q.when(x, function (x) { +var x = q.all([1, 2, 3].map(eventually)); +q.when(x, function (x) { console.log(x); }); -Q.all([ +q.all([ eventually(10), eventually(20) ]) @@ -32,7 +34,7 @@ Q.all([ console.log(x, y); }); -Q.fcall(function () { }) +q.fcall(function () { }) .then(function () { }) .then(function () { }) .then(function () { }) @@ -42,7 +44,7 @@ Q.fcall(function () { }) // Handle any error from step1 through step4 }).done(); -Q.allResolved([]) +q.allResolved([]) .then(function (promises: Qpromise[]) { promises.forEach(function (promise) { if (promise.isFulfilled()) { @@ -55,20 +57,20 @@ Q.allResolved([]) var initialVal: any; var funcs = ['foo', 'bar', 'baz', 'qux']; -var result = Q.resolve(initialVal); +var result = q.resolve(initialVal); funcs.forEach(function (f) { result = result.then(f); }); var replaceText = (text: string) => text.replace("a", "b"); -Q.nfcall(fs.readFile, "foo.txt", "utf-8").then(replaceText); +q.nfcall(fs.readFile, "foo.txt", "utf-8").then(replaceText); -Q.ninvoke(fs, "readFile", "foo.txt", "utf-8").then(replaceText); +q.ninvoke(fs, "readFile", "foo.txt", "utf-8").then(replaceText); -var deferred = Q.defer(); +var deferred = q.defer(); fs.readFile("foo.txt", "utf-8", deferred.makeNodeResolver()); deferred.promise.then(replaceText); -var readFile = Q.nfbind(fs.readFile); +var readFile = q.nfbind(fs.readFile); readFile("foo.txt", "utf-8").then(replaceText); \ No newline at end of file diff --git a/q/q.module.d.ts b/q/q.module.d.ts deleted file mode 100644 index 98e1dd9dc..000000000 --- a/q/q.module.d.ts +++ /dev/null @@ -1,31 +0,0 @@ -/// - -declare module "q" { - export function when(value: any, onFulfilled: Function, onRejected?: Function): Qpromise; - export function try(method: Function, ...args: any[]): Qpromise; - export function fbind(method: Function, ...args: any[]): Qpromise; - export function fcall(method: Function, ...args: any[]): Qpromise; - export function nfbind(nodeFunction: Function): (...args: any[]) => Qpromise; - export function nfcall(nodeFunction: Function, ...args: any[]): Qpromise; - export function ninvoke(nodeModule: any, functionName: string, ...args: any[]): Qpromise; - export function all(promises: Qpromise[]): Qpromise; - export function allResolved(promises: Qpromise[]): Qpromise; - export function spread(onFulfilled: Function, onRejected: Function): Qpromise; - export function timeout(ms: number): Qpromise; - export function delay(ms: number): Qpromise; - export function delay(value: any, ms: number): Qpromise; - export function isFulfilled(): bool; - export function isRejected(): bool; - export function isPending(): bool; - export function valueOf(): any; - export function defer(): Qdeferred; - export function (value: any): Qpromise; - export function reject(): Qpromise; - export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Qpromise; - export function isPromise(value: any): bool; - export function async(generatorFunction: any): Qdeferred; - export function nextTick(callback: Function); - export var oneerror: any; - export var longStackJumpLimit: number; - export function resolve(object?:Qpromise); -} \ No newline at end of file From a1223529d4a43f171861ea629c06062e19cf5bf1 Mon Sep 17 00:00:00 2001 From: Andrew Gaspar Date: Wed, 19 Jun 2013 23:30:21 -0700 Subject: [PATCH 2/7] Updated q so that the interfaces fell underneath the modules --- q/Q-tests.ts | 2 +- q/Q.d.ts | 102 ++++++++++++++++++++++---------------------- q/q.module-tests.ts | 3 +- 3 files changed, 53 insertions(+), 54 deletions(-) diff --git a/q/Q-tests.ts b/q/Q-tests.ts index 65a19d008..cd3cc653e 100644 --- a/q/Q-tests.ts +++ b/q/Q-tests.ts @@ -40,7 +40,7 @@ Q.fcall(function () { }) }).done(); Q.allResolved([]) -.then(function (promises: Qpromise[]) { +.then(function (promises: Q.Promise[]) { promises.forEach(function (promise) { if (promise.isFulfilled()) { var value = promise.valueOf(); diff --git a/q/Q.d.ts b/q/Q.d.ts index f1835241d..a0ecb75ce 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -1,68 +1,68 @@ // Type definitions for Q // Project: https://github.com/kriskowal/q -// Definitions by: Barrie Nemetchek +// Definitions by: Barrie Nemetchek, Andrew Gaspar // Definitions: https://github.com/borisyankov/DefinitelyTyped -interface Qdeferred { - promise: Qpromise; - resolve(value: any): any; - reject(reason: any); - notify(value: any); - makeNodeResolver(): () => void; -} - -interface Qpromise { - fail(errorCallback: Function): Qpromise; - fin(finallyCallback: Function): Qpromise; - then(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): Qpromise; - spread(onFulfilled: Function, onRejected?: Function): Qpromise; - catch(onRejected: Function): Qpromise; - progress(onProgress: Function): Qpromise; - done(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): Qpromise; - get (propertyName: String): Qpromise; - set (propertyName: String, value: any): Qpromise; - delete (propertyName: String): Qpromise; - post(methodName: String, args: any[]): Qpromise; - invoke(methodName: String, ...args: any[]): Qpromise; - keys(): Qpromise; - fapply(args: any[]): Qpromise; - fcall(method: Function, ...args: any[]): Qpromise; - timeout(ms: number): Qpromise; - delay(ms: number): Qpromise; - isFulfilled(): bool; - isRejected(): bool; - isPending(): bool; - valueOf(): any; -} - -declare function Q(value: any): Qpromise; +declare function Q(value: any): Q.Promise; declare module Q { - export function when(value: any, onFulfilled: Function, onRejected?: Function): Qpromise; + interface Deferred { + promise: Promise; + resolve(value: any): any; + reject(reason: any); + notify(value: any); + makeNodeResolver(): () => void; + } + + interface Promise { + fail(errorCallback: Function): Promise; + fin(finallyCallback: Function): Promise; + then(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): Promise; + spread(onFulfilled: Function, onRejected?: Function): Promise; + catch(onRejected: Function): Promise; + progress(onProgress: Function): Promise; + done(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): Promise; + 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; + keys(): Promise; + fapply(args: any[]): Promise; + fcall(method: Function, ...args: any[]): Promise; + timeout(ms: number): Promise; + delay(ms: number): Promise; + isFulfilled(): bool; + isRejected(): bool; + isPending(): bool; + valueOf(): any; + } + + export function when(value: any, onFulfilled: Function, onRejected?: Function): Promise; //export function try(method: Function, ...args: any[]): Qpromise; <- This is broken currently - not sure how to fix. - export function fbind(method: Function, ...args: any[]): Qpromise; - export function fcall(method: Function, ...args: any[]): Qpromise; - export function nfbind(nodeFunction: Function): (...args: any[]) => Qpromise; - export function nfcall(nodeFunction: Function, ...args: any[]): Qpromise; - export function ninvoke(nodeModule: any, functionName: string, ...args: any[]): Qpromise; - export function all(promises: Qpromise[]): Qpromise; - export function allResolved(promises: Qpromise[]): Qpromise; - export function spread(onFulfilled: Function, onRejected: Function): Qpromise; - export function timeout(ms: number): Qpromise; - export function delay(ms: number): Qpromise; - export function delay(value: any, ms: number): Qpromise; + 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(ms: number): Promise; + export function delay(ms: number): Promise; + export function delay(value: any, ms: number): Promise; export function isFulfilled(): bool; export function isRejected(): bool; export function isPending(): bool; export function valueOf(): any; - export function defer(): Qdeferred; - export function reject(): Qpromise; - export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Qpromise; + export function defer(): Deferred; + export function reject(): Promise; + export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Promise; export function isPromise(value: any): bool; - export function async(generatorFunction: any): Qdeferred; + export function async(generatorFunction: any): Deferred; export function nextTick(callback: Function); export var oneerror: any; export var longStackJumpLimit: number; - export function resolve(object?: Qpromise); + export function resolve(object?: Promise); } declare module "q" { diff --git a/q/q.module-tests.ts b/q/q.module-tests.ts index 2d32febca..fe4764e76 100644 --- a/q/q.module-tests.ts +++ b/q/q.module-tests.ts @@ -44,8 +44,7 @@ q.fcall(function () { }) // Handle any error from step1 through step4 }).done(); -q.allResolved([]) -.then(function (promises: Qpromise[]) { +q.allResolved([]).then(function (promises) { promises.forEach(function (promise) { if (promise.isFulfilled()) { var value = promise.valueOf(); From c37c38a984be1458edf7bec61012a2090ccb813f Mon Sep 17 00:00:00 2001 From: Andrew Gaspar Date: Thu, 20 Jun 2013 00:36:26 -0700 Subject: [PATCH 3/7] Added basic typings on q. TypeScript compiler bugs and spec limitations hinder full typing. --- q/Q-tests.ts | 8 ++--- q/Q.d.ts | 75 +++++++++++++++++++++++---------------------- q/q.module-tests.ts | 4 +-- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/q/Q-tests.ts b/q/Q-tests.ts index cd3cc653e..b04067003 100644 --- a/q/Q-tests.ts +++ b/q/Q-tests.ts @@ -1,6 +1,6 @@ /// -Q(8).then(x => console.log(x)); +Q(8).then(x => console.log(x.toExponential())); var delay = function (delay) { var d = Q.defer(); @@ -24,11 +24,11 @@ Q.when(x, function (x) { Q.all([ eventually(10), eventually(20) -]) -.spread(function (x, y) { +]).spread(function (x, y) { console.log(x, y); }); + Q.fcall(function () { }) .then(function () { }) .then(function () { }) @@ -40,7 +40,7 @@ Q.fcall(function () { }) }).done(); Q.allResolved([]) -.then(function (promises: Q.Promise[]) { +.then(function (promises: Q.Promise[]) { promises.forEach(function (promise) { if (promise.isFulfilled()) { var value = promise.valueOf(); diff --git a/q/Q.d.ts b/q/Q.d.ts index a0ecb75ce..25ed1b8e8 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -3,66 +3,67 @@ // Definitions by: Barrie Nemetchek, Andrew Gaspar // Definitions: https://github.com/borisyankov/DefinitelyTyped -declare function Q(value: any): Q.Promise; +declare function Q(value: T): Q.Promise; +//declare function Q(value: Q.Promise): Q.Promise declare module Q { - interface Deferred { - promise: Promise; + interface Deferred { + promise: Promise; resolve(value: any): any; reject(reason: any); notify(value: any); makeNodeResolver(): () => void; } - interface Promise { - fail(errorCallback: Function): Promise; - fin(finallyCallback: Function): Promise; - then(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): Promise; - spread(onFulfilled: Function, onRejected?: Function): Promise; - catch(onRejected: Function): Promise; - progress(onProgress: Function): Promise; - done(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): Promise; - 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; - keys(): Promise; - fapply(args: any[]): Promise; - fcall(method: Function, ...args: any[]): Promise; - timeout(ms: number): Promise; - delay(ms: number): Promise; + interface Promise { + fail(errorCallback: Function): Promise; + fin(finallyCallback: Function): Promise; + then(onFulfilled?: (value: T) => any, onRejected?: (reason) => any, onProgress?: Function): Promise; + spread(onFulfilled: Function, onRejected?: Function): Promise; + catch(onRejected: Function): Promise; + progress(onProgress: Function): Promise; + done(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): Promise; + 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; + keys(): Promise; + fapply(args: any[]): Promise; + fcall(method: Function, ...args: any[]): Promise; + timeout(ms: number): Promise; + delay(ms: number): Promise; isFulfilled(): bool; isRejected(): bool; isPending(): bool; valueOf(): any; } - export function when(value: any, onFulfilled: Function, onRejected?: Function): Promise; - //export function try(method: Function, ...args: any[]): Qpromise; <- 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(ms: number): Promise; - export function delay(ms: number): Promise; - export function delay(value: any, ms: number): Promise; + export function when(value: any, onFulfilled: Function, onRejected?: Function): 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(ms: number): Promise; + export function delay(ms: number): Promise; + export function delay(value: any, ms: number): Promise; export function isFulfilled(): bool; export function isRejected(): bool; export function isPending(): bool; export function valueOf(): any; export function defer(): Deferred; - export function reject(): Promise; - export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Promise; + export function reject(): Promise; + export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Promise; export function isPromise(value: any): bool; export function async(generatorFunction: any): Deferred; export function nextTick(callback: Function); export var oneerror: any; export var longStackJumpLimit: number; - export function resolve(object?: Promise); + export function resolve(object?: Promise); } declare module "q" { diff --git a/q/q.module-tests.ts b/q/q.module-tests.ts index fe4764e76..037dd028c 100644 --- a/q/q.module-tests.ts +++ b/q/q.module-tests.ts @@ -5,7 +5,7 @@ import q = module("q"); import fs = module("fs"); -q(8).then(x => console.log(x)); +q(8).then(x => console.log(x.toExponential())); var delay = function (delay) { var d = q.defer(); @@ -44,7 +44,7 @@ q.fcall(function () { }) // Handle any error from step1 through step4 }).done(); -q.allResolved([]).then(function (promises) { +q.allResolved([]).then(function (promises: Q.Promise[]) { promises.forEach(function (promise) { if (promise.isFulfilled()) { var value = promise.valueOf(); From 9c38124670c075a482e542dde5f2d7694872b8fe Mon Sep 17 00:00:00 2001 From: Andrew Gaspar Date: Thu, 20 Jun 2013 00:43:35 -0700 Subject: [PATCH 4/7] Added typing to Deferred and defer() --- q/Q-tests.ts | 2 +- q/Q.d.ts | 6 +++--- q/q.module-tests.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/q/Q-tests.ts b/q/Q-tests.ts index b04067003..02a409b6b 100644 --- a/q/Q-tests.ts +++ b/q/Q-tests.ts @@ -2,7 +2,7 @@ Q(8).then(x => console.log(x.toExponential())); -var delay = function (delay) { +var delay = function (delay: number) { var d = Q.defer(); setTimeout(d.resolve, delay); return d.promise; diff --git a/q/Q.d.ts b/q/Q.d.ts index 25ed1b8e8..951f615d0 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -8,10 +8,10 @@ declare function Q(value: T): Q.Promise; declare module Q { interface Deferred { promise: Promise; - resolve(value: any): any; + resolve(value: T): any; reject(reason: any); notify(value: any); - makeNodeResolver(): () => void; + makeNodeResolver(): (reason, value: T) => void; } interface Promise { @@ -55,7 +55,7 @@ declare module Q { export function isRejected(): bool; export function isPending(): bool; export function valueOf(): any; - export function defer(): Deferred; + export function defer(): Deferred; export function reject(): Promise; export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Promise; export function isPromise(value: any): bool; diff --git a/q/q.module-tests.ts b/q/q.module-tests.ts index 037dd028c..a589ca1d6 100644 --- a/q/q.module-tests.ts +++ b/q/q.module-tests.ts @@ -67,7 +67,7 @@ q.nfcall(fs.readFile, "foo.txt", "utf-8").then(replaceText); q.ninvoke(fs, "readFile", "foo.txt", "utf-8").then(replaceText); -var deferred = q.defer(); +var deferred = q.defer(); fs.readFile("foo.txt", "utf-8", deferred.makeNodeResolver()); deferred.promise.then(replaceText); From 91a4452e37e0c97ac17b991b7e22d4e979da82bf Mon Sep 17 00:00:00 2001 From: Andrew Gaspar Date: Thu, 20 Jun 2013 22:03:23 -0700 Subject: [PATCH 5/7] Removed separate q module tests and updated with some addition typing information for Q.d.ts. --- q/Q-tests.ts | 17 +++++----- q/Q.d.ts | 44 ++++++++++++++------------ q/q.module-tests.ts | 75 --------------------------------------------- 3 files changed, 34 insertions(+), 102 deletions(-) delete mode 100644 q/q.module-tests.ts diff --git a/q/Q-tests.ts b/q/Q-tests.ts index 02a409b6b..f982ae8d6 100644 --- a/q/Q-tests.ts +++ b/q/Q-tests.ts @@ -1,5 +1,7 @@ /// +import q = module('q'); + Q(8).then(x => console.log(x.toExponential())); var delay = function (delay: number) { @@ -12,6 +14,14 @@ Q.when(delay(1000), function () { console.log('Hello, World!'); }); +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); +Q.delay("asdf", 1000).then(x => x.length); + +var eventualAdd = Q.promised((a: number, b: number) => a + b); +eventualAdd(Q(1), Q(2)).then(x => x.toExponential()); + var eventually = function (eventually) { return Q.delay(eventually, 1000); }; @@ -48,11 +58,4 @@ Q.allResolved([]) var exception = promise.valueOf().exception; } }) -}); - -var initialVal: any; -var funcs = ['foo', 'bar', 'baz', 'qux']; -var result = Q.resolve(initialVal); -funcs.forEach(function (f) { - result = result.then(f); }); \ No newline at end of file diff --git a/q/Q.d.ts b/q/Q.d.ts index 951f615d0..56a070b35 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -16,22 +16,23 @@ declare module Q { interface Promise { fail(errorCallback: Function): Promise; - fin(finallyCallback: Function): Promise; + fin(finallyCallback: Function): Promise; + finally(finallyCallback: Function): Promise; then(onFulfilled?: (value: T) => any, onRejected?: (reason) => any, onProgress?: Function): Promise; spread(onFulfilled: Function, onRejected?: Function): Promise; catch(onRejected: Function): Promise; progress(onProgress: Function): Promise; - done(onFulfilled?: Function, onRejected?: Function, 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; - keys(): Promise; + keys(): Promise; fapply(args: any[]): Promise; fcall(method: Function, ...args: any[]): Promise; - timeout(ms: number): Promise; - delay(ms: number): Promise; + timeout(ms: number, message?): Promise; + delay(ms: number): Promise; isFulfilled(): bool; isRejected(): bool; isPending(): bool; @@ -48,22 +49,25 @@ declare module Q { export function all(promises: Promise[]): Promise; export function allResolved(promises: Promise[]): Promise; export function spread(onFulfilled: Function, onRejected: Function): Promise; - export function timeout(ms: number): Promise; - export function delay(ms: number): Promise; - export function delay(value: any, ms: number): Promise; - export function isFulfilled(): bool; - export function isRejected(): bool; - export function isPending(): bool; - export function valueOf(): any; + export function timeout(promise: Promise, ms: number, message?): Promise; + export function delay(promise: Promise, ms: number): Promise; + export function delay(value: T, ms: number): Promise; + export function isFulfilled(promise: Promise): bool; + export function isRejected(promise: Promise): bool; + export function isPending(promise: Promise): bool; + export function valueOf(promise: Promise): T; export function defer(): Deferred; - export function reject(): Promise; - export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Promise; - export function isPromise(value: any): bool; - export function async(generatorFunction: any): Deferred; - export function nextTick(callback: Function); - export var oneerror: any; - export var longStackJumpLimit: number; - export function resolve(object?: Promise); + export function reject(reason?): Promise; + export function promise(factory: { resolve: Function; reject: Function; notify: Function; }): Promise; + export function promised(callback: (...any) => T): (...any) => Promise; + export function isPromise(object): bool; + export function isPromiseAlike(object): bool; + export function isPending(object): bool; + export function async(generatorFunction: any): (...args) => Promise; + export function nextTick(callback: Function): void; + export var oneerror: () => void; + export var longStackSupport: bool; + export function resolve(object): Promise; } declare module "q" { diff --git a/q/q.module-tests.ts b/q/q.module-tests.ts deleted file mode 100644 index a589ca1d6..000000000 --- a/q/q.module-tests.ts +++ /dev/null @@ -1,75 +0,0 @@ -/// -/// -/// - -import q = module("q"); -import fs = module("fs"); - -q(8).then(x => console.log(x.toExponential())); - -var delay = function (delay) { - var d = q.defer(); - setTimeout(d.resolve, delay); - return d.promise; -}; - -q.when(delay(1000), function () { - console.log('Hello, World!'); -}); - -var eventually = function (eventually) { - return q.delay(eventually, 1000); -}; - -var x = q.all([1, 2, 3].map(eventually)); -q.when(x, function (x) { - console.log(x); -}); - -q.all([ - eventually(10), - eventually(20) -]) -.spread(function (x, y) { - console.log(x, y); -}); - -q.fcall(function () { }) -.then(function () { }) -.then(function () { }) -.then(function () { }) -.then(function (value4) { - // Do something with value4 -}, function (error) { - // Handle any error from step1 through step4 -}).done(); - -q.allResolved([]).then(function (promises: Q.Promise[]) { - promises.forEach(function (promise) { - if (promise.isFulfilled()) { - var value = promise.valueOf(); - } else { - var exception = promise.valueOf().exception; - } - }) -}); - -var initialVal: any; -var funcs = ['foo', 'bar', 'baz', 'qux']; -var result = q.resolve(initialVal); -funcs.forEach(function (f) { - result = result.then(f); -}); - -var replaceText = (text: string) => text.replace("a", "b"); - -q.nfcall(fs.readFile, "foo.txt", "utf-8").then(replaceText); - -q.ninvoke(fs, "readFile", "foo.txt", "utf-8").then(replaceText); - -var deferred = q.defer(); -fs.readFile("foo.txt", "utf-8", deferred.makeNodeResolver()); -deferred.promise.then(replaceText); - -var readFile = q.nfbind(fs.readFile); -readFile("foo.txt", "utf-8").then(replaceText); \ No newline at end of file From d4b888e50fc7626f64d220ad14db1a2681f125c7 Mon Sep 17 00:00:00 2001 From: Andrew Gaspar Date: Thu, 20 Jun 2013 22:06:09 -0700 Subject: [PATCH 6/7] Slight update to q.d.ts to reflect current limitations in TypeScript. --- q/Q.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/q/Q.d.ts b/q/Q.d.ts index 56a070b35..dd6786594 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -3,8 +3,8 @@ // Definitions by: Barrie Nemetchek, Andrew Gaspar // Definitions: https://github.com/borisyankov/DefinitelyTyped -declare function Q(value: T): Q.Promise; -//declare function Q(value: Q.Promise): Q.Promise +declare function Q(value): Q.Promise; + declare module Q { interface Deferred { promise: Promise; From 41c216fb10e2da757f55d1ee2d1d0035b7f3e768 Mon Sep 17 00:00:00 2001 From: Andrew Gaspar Date: Thu, 20 Jun 2013 22:25:55 -0700 Subject: [PATCH 7/7] Changed bool to boolean for q.d.ts --- q/Q.d.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/q/Q.d.ts b/q/Q.d.ts index dd6786594..9fd4518f2 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -33,9 +33,9 @@ declare module Q { fcall(method: Function, ...args: any[]): Promise; timeout(ms: number, message?): Promise; delay(ms: number): Promise; - isFulfilled(): bool; - isRejected(): bool; - isPending(): bool; + isFulfilled(): boolean; + isRejected(): boolean; + isPending(): boolean; valueOf(): any; } @@ -52,21 +52,21 @@ declare module Q { export function timeout(promise: Promise, ms: number, message?): Promise; export function delay(promise: Promise, ms: number): Promise; export function delay(value: T, ms: number): Promise; - export function isFulfilled(promise: Promise): bool; - export function isRejected(promise: Promise): bool; - export function isPending(promise: Promise): bool; + 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 promised(callback: (...any) => T): (...any) => Promise; - export function isPromise(object): bool; - export function isPromiseAlike(object): bool; - export function isPending(object): bool; + 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: bool; + export var longStackSupport: boolean; export function resolve(object): Promise; }