Merge pull request #656 from AndrewGaspar/master

Updated Q with basic typings - limited by current TypeScript limitations
This commit is contained in:
Diullei Gomes
2013-06-20 22:35:08 -07:00
4 changed files with 85 additions and 174 deletions
+16 -11
View File
@@ -1,6 +1,10 @@
/// <reference path="Q.d.ts" />
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<any>[]) {
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);
});
Vendored
+69 -58
View File
@@ -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<T>(value): Q.Promise<T>;
declare module Q {
interface Deferred<T> {
promise: Promise<T>;
resolve(value: T): any;
reject(reason: any);
notify(value: any);
makeNodeResolver(): (reason, value: T) => void;
}
interface Promise<T> {
fail(errorCallback: Function): Promise<any>;
fin(finallyCallback: Function): Promise<T>;
finally(finallyCallback: Function): Promise<T>;
then(onFulfilled?: (value: T) => any, onRejected?: (reason) => any, onProgress?: Function): Promise<any>;
spread(onFulfilled: Function, onRejected?: Function): Promise<any>;
catch(onRejected: Function): Promise<any>;
progress(onProgress: Function): Promise<any>;
done(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): void;
get(propertyName: String): Promise<any>;
set(propertyName: String, value: any): Promise<any>;
delete(propertyName: String): Promise<any>;
post(methodName: String, args: any[]): Promise<any>;
invoke(methodName: String, ...args: any[]): Promise<any>;
keys(): Promise<string[]>;
fapply(args: any[]): Promise<any>;
fcall(method: Function, ...args: any[]): Promise<any>;
timeout(ms: number, message?): Promise<T>;
delay(ms: number): Promise<T>;
isFulfilled(): boolean;
isRejected(): boolean;
isPending(): boolean;
valueOf(): any;
}
export function when(value: any, onFulfilled: Function, onRejected?: Function): Promise<any>;
//export function try(method: Function, ...args: any[]): Promise<any>; // <- This is broken currently - not sure how to fix.
export function fbind(method: Function, ...args: any[]): Promise<any>;
export function fcall(method: Function, ...args: any[]): Promise<any>;
export function nfbind(nodeFunction: Function): (...args: any[]) => Promise<any>;
export function nfcall(nodeFunction: Function, ...args: any[]): Promise<any>;
export function ninvoke(nodeModule: any, functionName: string, ...args: any[]): Promise<any>;
export function all(promises: Promise<any>[]): Promise<any>;
export function allResolved(promises: Promise<any>[]): Promise<any>;
export function spread(onFulfilled: Function, onRejected: Function): Promise<any>;
export function timeout<T>(promise: Promise<T>, ms: number, message?): Promise<T>;
export function delay<T>(promise: Promise<T>, ms: number): Promise<T>;
export function delay<T>(value: T, ms: number): Promise<T>;
export function isFulfilled(promise: Promise<any>): boolean;
export function isRejected(promise: Promise<any>): boolean;
export function isPending(promise: Promise<any>): boolean;
export function valueOf<T>(promise: Promise<T>): T;
export function defer<T>(): Deferred<T>;
export function reject(reason?): Promise<any>;
export function promise<T>(factory: { resolve: Function; reject: Function; notify: Function; }): Promise<T>;
export function promised<T>(callback: (...any) => T): (...any) => Promise<T>;
export function isPromise(object): boolean;
export function isPromiseAlike(object): boolean;
export function isPending(object): boolean;
export function async<T>(generatorFunction: any): (...args) => Promise<T>;
export function nextTick(callback: Function): void;
export var oneerror: () => void;
export var longStackSupport: boolean;
export function resolve<T>(object): Promise<T>;
}
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;
}
-74
View File
@@ -1,74 +0,0 @@
/// <reference path="q.module.d.ts" />
/// <reference path="../jasmine/jasmine.d.ts" />
/// <reference path="../node/node.d.ts" />
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);
-31
View File
@@ -1,31 +0,0 @@
/// <reference path="Q.d.ts" />
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);
}