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..98e1dd9dc8 100644 --- a/q/q.module.d.ts +++ b/q/q.module.d.ts @@ -1,10 +1,13 @@ /// -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; 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;