es6-promises defintion addition

This commit is contained in:
François de Campredon
2014-01-15 13:12:37 +01:00
parent 7eb9bd90e9
commit 5b5846cbe4
3 changed files with 116 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
var promiseString: Promise<string>,
promiseStringArr: Promise<string[]>,
arrayOfPromise: Promise<string>[],
promiseNumber: Promise<number>,
promiseAny: Promise<any>,
thenable: Thenable<string>;
// constructor test
var constructResult = new Promise<string>((resolve, reject) => {
resolve('a string');
});
promiseString = constructResult;
var constructResult1 = new Promise<string>((resolve:(promise: Thenable<string>) => void , reject) => {
resolve(Promise.resolve('a string'));
});
promiseString = constructResult1;
//cast test
var castResult = Promise.cast('a string');
promiseString = castResult;
var castResult1 = Promise.cast(Promise.resolve('a string'));
promiseString = castResult1;
//resolve test
var resolveResult = Promise.resolve('a string');
promiseString = resolveResult;
var resolveResult1 = Promise.resolve(thenable);
promiseString = resolveResult1;
//reject test
var rejectResult = Promise.reject('there is an error');
promiseAny = rejectResult;
//all test
var allResult = Promise.all(arrayOfPromise);
promiseStringArr = allResult;
//race test
var raceResult = Promise.race(arrayOfPromise);
promiseString = raceResult;
//then test
var thenWithPromiseResult = promiseString.then(word => Promise.resolve(word.length));
promiseNumber = thenWithPromiseResult;
var thenWithPromiseResultAndVoidReject = promiseString.then(word => Promise.resolve(word.length), error => console.log(error));
promiseNumber = thenWithPromiseResultAndVoidReject;
var thenWithPromiseResultAndPromiseReject = promiseString.then(word => Promise.resolve(word.length), error => Promise.resolve(10));
promiseNumber = thenWithPromiseResultAndPromiseReject;
var thenWithPromiseResultAndSimpleReject = promiseString.then(word => Promise.resolve(word.length), error => 10);
promiseNumber = thenWithPromiseResultAndSimpleReject;
var thenWithSimpleResult = promiseString.then(word => word.length);
promiseNumber = thenWithSimpleResult;
var thenWithSimpleResultAndVoidReject = promiseString.then(word => word.length, error => console.log(error));
promiseNumber = thenWithSimpleResultAndVoidReject;
var thenWithSimpleResultAndPromiseReject = promiseString.then(word => word.length, error => Promise.resolve(10));
promiseNumber = thenWithSimpleResultAndPromiseReject;
var thenWithSimpleResultAndSimpleReject = promiseString.then(word => word.length, error => 10);
promiseNumber = thenWithSimpleResultAndSimpleReject;
//catch test
var catchWithSimpleResult = promiseString.catch(error => 10);
promiseNumber = catchWithSimpleResult;
var catchWithPromiseResult = promiseString.catch(error => Promise.resolve(10));
promiseNumber = catchWithPromiseResult;
+1
View File
@@ -0,0 +1 @@
""
+39
View File
@@ -0,0 +1,39 @@
interface Thenable<R> {
then<U>(onFulfill: (value: R) => Thenable<U>, onReject?: (error: any) => void): Thenable<U>;
then<U>(onFulfill: (value: R) => U, onReject?: (error: any) => void): Thenable<U>;
then<U>(onFulfill: (value: R) => Thenable<U>, onReject?: (error: any) => Thenable<U>): Thenable<U>;
then<U>(onFulfill: (value: R) => Thenable<U>, onReject?: (error: any) => U): Thenable<U>;
then<U>(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable<U>): Thenable<U>;
then<U>(onFulfill: (value: R) => U, onReject?: (error: any) => U): Thenable<U>;
}
declare class Promise<R> implements Thenable<R> {
constructor(callback: (resolve : (result: R) => void, reject: (error: any) => void) => void);
constructor(callback: (resolve : (result: Thenable<R>) => void, reject: (error: any) => void) => void);
then<U>(onFulfill: (value: R) => Thenable<U>, onReject?: (error: any) => void): Promise<U>;
then<U>(onFulfill: (value: R) => U, onReject?: (error: any) => void): Promise<U>;
then<U>(onFulfill: (value: R) => Thenable<U>, onReject?: (error: any) => Thenable<U>): Promise<U>;
then<U>(onFulfill: (value: R) => Thenable<U>, onReject?: (error: any) => U): Promise<U>;
then<U>(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable<U>): Promise<U>;
then<U>(onFulfill: (value: R) => U, onReject?: (error: any) => U): Promise<U>;
catch<U>(onReject?: (error: any) => Thenable<U>): Promise<U>
catch<U>(onReject?: (error: any) => U): Promise<U>
}
declare module Promise {
function cast<R>(promise: Promise<R>): Promise<R>;
function cast<R>(object: R): Promise<R>;
function resolve<R>(thenable: Thenable<R>): Promise<R>;
function resolve<R>(object: R): Promise<R>;
function reject(error: any): Promise<any>;
function all<R>(promises: Promise<R>[]): Promise<R[]>;
function race<R>(promises: Promise<R>[]): Promise<R>;
}