mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-21 21:31:12 +08:00
Updated the q module with node adapter functions.
This commit is contained in:
6
q/Q.d.ts
vendored
6
q/Q.d.ts
vendored
@@ -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;
|
||||
|
||||
@@ -1,30 +1,9 @@
|
||||
/// <reference path="q.module.d.ts" />
|
||||
/// <reference path="../jasmine/jasmine.d.ts" />
|
||||
/// <reference path="../node/node.d.ts" />
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
3
q/q.module.d.ts
vendored
3
q/q.module.d.ts
vendored
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user