mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-04 19:42:46 +08:00
Q definitions updated
This commit is contained in:
49
Q/Q-tests.ts
Normal file
49
Q/Q-tests.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/// <reference path="Q.d.ts" />
|
||||
|
||||
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;
|
||||
}
|
||||
})
|
||||
});
|
||||
6
Q/Q.d.ts
vendored
6
Q/Q.d.ts
vendored
@@ -8,16 +8,17 @@ interface Qdeferred {
|
||||
resolve(value: any): any;
|
||||
reject(reason: any);
|
||||
notify(value: any);
|
||||
makeNodeResolver();
|
||||
makeNodeResolver(): Function;
|
||||
}
|
||||
|
||||
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;
|
||||
done(onFulfilled?: Function, onRejected?: Function, onProgress?: Function): Qpromise;
|
||||
get (propertyName: String): Qpromise;
|
||||
set (propertyName: String, value: any): Qpromise;
|
||||
delete (propertyName: String): Qpromise;
|
||||
@@ -35,6 +36,7 @@ interface Qpromise {
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
75
Q/q.module-tests.ts
Normal file
75
Q/q.module-tests.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
/// <reference path="q.module.d.ts" />
|
||||
/// <reference path="../jasmine/jasmine.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));
|
||||
});
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
})
|
||||
});
|
||||
27
Q/q.module.d.ts
vendored
Normal file
27
Q/q.module.d.ts
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
/// <reference path="Q.d.ts" />
|
||||
|
||||
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 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;
|
||||
}
|
||||
Reference in New Issue
Block a user