From 5055ef38f4caea3511da830a96d86cb4c88b3c1e Mon Sep 17 00:00:00 2001 From: Andrew Gaspar Date: Thu, 30 May 2013 22:50:21 -0700 Subject: [PATCH 1/2] Updated the q module with node adapter functions. --- q/Q.d.ts | 6 +++--- q/q.module-tests.ts | 40 ++++++++++++++++------------------------ q/q.module.d.ts | 3 +++ 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/q/Q.d.ts b/q/Q.d.ts index 70c77434d3..fd81031fe1 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -8,13 +8,13 @@ interface Qdeferred { resolve(value: any): any; reject(reason: any); notify(value: any); - makeNodeResolver(): Function; + makeNodeResolver(): () => void; } interface Qpromise { fail(errorCallback: Function): Qpromise; fin(finallyCallback: Function): Qpromise; - then(onFulfilled: Function, onRejected?: Function, onProgress?: Function): Qpromise; + then(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): Qpromise; spread(onFulfilled: Function, onRejected?: Function): Qpromise; catch(onRejected: Function): Qpromise; progress(onProgress: Function): Qpromise; @@ -61,4 +61,4 @@ interface QStatic { oneerror: any; longStackJumpLimit: number; } -declare var Q: QStatic; +declare var Q: QStatic; diff --git a/q/q.module-tests.ts b/q/q.module-tests.ts index e90d17c2ec..4f9039d23f 100644 --- a/q/q.module-tests.ts +++ b/q/q.module-tests.ts @@ -1,30 +1,9 @@ /// /// +/// import Q = module("q"); - -describe("q", function () { - it("should return", function (done) { - Q({ myValue: true }).then(function (obj) { - - if (obj.myValue) done(); - else done("didn't work =("); - }, - (err) => done(err)); - }); - - it("should process all", function (done: (err?) => void ) { - Q.all([Q(1), Q(2), Q(3)]).then(function (arr: number[]) { - var sum = arr.reduce(function (memo, cur) { - return memo + cur; - }, 0); - - if (sum === 6) done(); - else done({ actual: sum }); - }, - (err) => done(err)); - }); -}); +import fs = module("fs"); var delay = function (delay) { var d = Q.defer(); @@ -79,4 +58,17 @@ 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 +}); + +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 index 3c88ec7379..6963e47b7c 100644 --- a/q/q.module.d.ts +++ b/q/q.module.d.ts @@ -5,6 +5,9 @@ module "q" { 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; From b232a67e3a2f298304c11c56821650e1a6e9d169 Mon Sep 17 00:00:00 2001 From: Andrew Gaspar Date: Thu, 30 May 2013 23:02:58 -0700 Subject: [PATCH 2/2] Change module to declare module --- q/q.module.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/q/q.module.d.ts b/q/q.module.d.ts index 6963e47b7c..98e1dd9dc8 100644 --- a/q/q.module.d.ts +++ b/q/q.module.d.ts @@ -1,6 +1,6 @@ /// -module "q" { +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;