mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-07 16:30:12 +08:00
76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
/// <reference path="../jquery/jquery.d.ts" />
|
|
/// <reference path="Q.d.ts" />
|
|
|
|
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;
|
|
};
|
|
|
|
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);
|
|
};
|
|
|
|
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: Q.Promise<any>[]) {
|
|
promises.forEach(function (promise) {
|
|
if (promise.isFulfilled()) {
|
|
var value = promise.valueOf();
|
|
} else {
|
|
var exception = promise.valueOf().exception;
|
|
}
|
|
})
|
|
});
|
|
|
|
declare var arrayPromise: Q.IPromise<number[]>;
|
|
declare var stringPromise: Q.IPromise<string>;
|
|
declare function returnsNumPromise(text: string): Q.Promise<number>;
|
|
|
|
Q<number[]>(arrayPromise) // type specification required
|
|
.then(arr => arr.join(','))
|
|
.then<number>(returnsNumPromise) // requires specification
|
|
.then(num => num.toFixed());
|
|
|
|
declare var jPromise: JQueryPromise;
|
|
|
|
// if jQuery promises definition supported generics, this could be more interesting example
|
|
Q<any>(jPromise).then((val) => val.toExponential()); |