diff --git a/q/Q-tests.ts b/q/Q-tests.ts index 34cde9341..f982ae8d6 100644 --- a/q/Q-tests.ts +++ b/q/Q-tests.ts @@ -1,6 +1,10 @@ /// -var delay = function (delay) { +import q = module('q'); + +Q(8).then(x => console.log(x.toExponential())); + +var delay = function (delay: number) { var d = Q.defer(); setTimeout(d.resolve, delay); return d.promise; @@ -10,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); }; @@ -22,11 +34,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 () { }) @@ -38,7 +50,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(); @@ -46,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 15f3fdea7..9fd4518f2 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -1,64 +1,75 @@ // 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; +declare function Q(value): Q.Promise; + +declare module Q { + interface Deferred { + promise: Promise; + resolve(value: T): any; + reject(reason: any); + notify(value: any); + makeNodeResolver(): (reason, value: T) => void; + } + + interface Promise { + fail(errorCallback: 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): 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; + fapply(args: any[]): Promise; + fcall(method: Function, ...args: any[]): Promise; + timeout(ms: number, message?): 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 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 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 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; } -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; -} - -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 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 deleted file mode 100644 index 4f9039d23..000000000 --- a/q/q.module-tests.ts +++ /dev/null @@ -1,74 +0,0 @@ -/// -/// -/// - -import Q = module("q"); -import fs = module("fs"); - -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: Qpromise[]) { - 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 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