From 5b5846cbe46da6b64d5dce1a077307334d54bdd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Campredon?= Date: Wed, 15 Jan 2014 13:12:37 +0100 Subject: [PATCH 1/7] es6-promises defintion addition --- es6-promises/promises-tests.ts | 76 ++++++++++++++++++++++++ es6-promises/promises-tests.ts.tscparams | 1 + es6-promises/promises.d.ts | 39 ++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 es6-promises/promises-tests.ts create mode 100644 es6-promises/promises-tests.ts.tscparams create mode 100644 es6-promises/promises.d.ts diff --git a/es6-promises/promises-tests.ts b/es6-promises/promises-tests.ts new file mode 100644 index 000000000..9f1e05dc2 --- /dev/null +++ b/es6-promises/promises-tests.ts @@ -0,0 +1,76 @@ +var promiseString: Promise, + promiseStringArr: Promise, + arrayOfPromise: Promise[], + promiseNumber: Promise, + promiseAny: Promise, + thenable: Thenable; + +// constructor test +var constructResult = new Promise((resolve, reject) => { + resolve('a string'); +}); +promiseString = constructResult; + + +var constructResult1 = new Promise((resolve:(promise: Thenable) => 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; diff --git a/es6-promises/promises-tests.ts.tscparams b/es6-promises/promises-tests.ts.tscparams new file mode 100644 index 000000000..3cc762b55 --- /dev/null +++ b/es6-promises/promises-tests.ts.tscparams @@ -0,0 +1 @@ +"" \ No newline at end of file diff --git a/es6-promises/promises.d.ts b/es6-promises/promises.d.ts new file mode 100644 index 000000000..c24844e6f --- /dev/null +++ b/es6-promises/promises.d.ts @@ -0,0 +1,39 @@ +interface Thenable { + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Thenable; + then(onFulfill: (value: R) => U, onReject?: (error: any) => void): Thenable; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => Thenable): Thenable; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Thenable; + then(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable): Thenable; + then(onFulfill: (value: R) => U, onReject?: (error: any) => U): Thenable; +} + +declare class Promise implements Thenable { + constructor(callback: (resolve : (result: R) => void, reject: (error: any) => void) => void); + constructor(callback: (resolve : (result: Thenable) => void, reject: (error: any) => void) => void); + + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Promise; + then(onFulfill: (value: R) => U, onReject?: (error: any) => void): Promise; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => Thenable): Promise; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Promise; + then(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable): Promise; + then(onFulfill: (value: R) => U, onReject?: (error: any) => U): Promise; + + catch(onReject?: (error: any) => Thenable): Promise + catch(onReject?: (error: any) => U): Promise +} + +declare module Promise { + + + function cast(promise: Promise): Promise; + function cast(object: R): Promise; + + function resolve(thenable: Thenable): Promise; + function resolve(object: R): Promise; + + function reject(error: any): Promise; + + function all(promises: Promise[]): Promise; + + function race(promises: Promise[]): Promise; +} \ No newline at end of file From 4887a99db3909a6a53348831cb551007d8dcaee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Campredon?= Date: Wed, 15 Jan 2014 16:58:14 +0100 Subject: [PATCH 2/7] Renaming to respect conventions, add Readme --- README.md | 1 + ...romises-tests.ts => es6-promises-tests.ts} | 2 + ...params => es6-promises-tests.ts.tscparams} | 0 es6-promises/es6-promises.d.ts | 95 +++++++++++++++++++ es6-promises/promises.d.ts | 39 -------- 5 files changed, 98 insertions(+), 39 deletions(-) rename es6-promises/{promises-tests.ts => es6-promises-tests.ts} (98%) rename es6-promises/{promises-tests.ts.tscparams => es6-promises-tests.ts.tscparams} (100%) create mode 100644 es6-promises/es6-promises.d.ts delete mode 100644 es6-promises/promises.d.ts diff --git a/README.md b/README.md index 27d6be4e4..d97c6c2d0 100755 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ List of Definitions * [EaselJS](http://www.createjs.com/#!/EaselJS) (by [Pedro Ferreira](https://bitbucket.org/drk4)) * [ember.js](http://emberjs.com/) (by [Boris Yankov](https://github.com/borisyankov)) * [EpicEditor](http://epiceditor.com/) (by [Boris Yankov](https://github.com/borisyankov)) +* [ES6-Promises](https://github.com/jakearchibald/ES6-Promises) (by [François de Campredon](https://github.com/fdecampredon/)) * [expect.js](https://github.com/LearnBoost/expect.js) (by [Teppei Sato](https://github.com/teppeis)) * [expectations](https://github.com/spmason/expectations) (by [vvakame](https://github.com/vvakame)) * [Express](http://expressjs.com/) (by [Boris Yankov](https://github.com/borisyankov)) diff --git a/es6-promises/promises-tests.ts b/es6-promises/es6-promises-tests.ts similarity index 98% rename from es6-promises/promises-tests.ts rename to es6-promises/es6-promises-tests.ts index 9f1e05dc2..fd1160572 100644 --- a/es6-promises/promises-tests.ts +++ b/es6-promises/es6-promises-tests.ts @@ -1,3 +1,5 @@ +/// + var promiseString: Promise, promiseStringArr: Promise, arrayOfPromise: Promise[], diff --git a/es6-promises/promises-tests.ts.tscparams b/es6-promises/es6-promises-tests.ts.tscparams similarity index 100% rename from es6-promises/promises-tests.ts.tscparams rename to es6-promises/es6-promises-tests.ts.tscparams diff --git a/es6-promises/es6-promises.d.ts b/es6-promises/es6-promises.d.ts new file mode 100644 index 000000000..fc013817d --- /dev/null +++ b/es6-promises/es6-promises.d.ts @@ -0,0 +1,95 @@ +// Type definitions for es6-promises +// Project: https://github.com/jakearchibald/ES6-Promises +// Definitions by: François de Campredon +// Definitions: https://github.com/borisyankov/DefinitelyTyped + + +interface Thenable { + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Thenable; + then(onFulfill: (value: R) => U, onReject?: (error: any) => void): Thenable; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => Thenable): Thenable; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Thenable; + then(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable): Thenable; + then(onFulfill: (value: R) => U, onReject?: (error: any) => U): Thenable; +} + +declare class Promise implements Thenable { + /** + * @param resolve Your promise is fulfilled with obj + * @rejectYour promise is rejected with obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error. Any errors thrown in the constructor callback will be implicitly passed to reject(). + */ + constructor(callback: (resolve : (result: R) => void, reject: (error: any) => void) => void); + /** + * @param resolve Your promise will be fulfilled/rejected with the outcome of thenable reject(obj) + * @rejectYour promise is rejected with obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error. Any errors thrown in the constructor callback will be implicitly passed to reject(). + */ + constructor(callback: (resolve : (result: Thenable) => void, reject: (error: any) => void) => void); + + + /** + * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. + * Both callbacks have a single parameter , the fulfillment value or rejection reason. + * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. + * If an error is thrown in the callback, the returned promise rejects with that error. + * + * @param onFullFill called when/if "promise" resolves + * @param onReject called when/if "promise" rejects + */ + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Promise; + then(onFulfill: (value: R) => U, onReject?: (error: any) => void): Promise; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => Thenable): Promise; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Promise; + then(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable): Promise; + then(onFulfill: (value: R) => U, onReject?: (error: any) => U): Promise; + + + /** + * Sugar for promise.then(undefined, onRejected) + * + * @param onReject called when/if "promise" rejects + */ + catch(onReject?: (error: any) => Thenable): Promise + catch(onReject?: (error: any) => U): Promise +} + +declare module Promise { + + /** + * Returns promise (only if promise.constructor == Promise) + */ + function cast(promise: Promise): Promise; + /** + * Make a promise that fulfills to obj. + */ + function cast(object: R): Promise; + + + /** + * Make a new promise from the thenable. + * A thenable is promise-like in as far as it has a "then" method. + * This also creates a new promise if you pass it a genuine JavaScript promise, making it less efficient for casting than Promise.cast. + */ + function resolve(thenable: Thenable): Promise; + /** + * Make a promise that fulfills to obj. Same as Promise.cast(obj) in this situation. + */ + function resolve(object: R): Promise; + + /** + * Make a promise that rejects to obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error + */ + function reject(error: any): Promise; + + /** + * Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects. + * Eadd array item is passed to Promise.cast, so the array can be a mixture of promise-like objects and other objects. + * The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value. + */ + function all(promises: Promise[]): Promise; + + /** + * Make a Promise that fulfills when any item fulfills, and rejects if any item rejects. + */ + function race(promises: Promise[]): Promise; +} \ No newline at end of file diff --git a/es6-promises/promises.d.ts b/es6-promises/promises.d.ts deleted file mode 100644 index c24844e6f..000000000 --- a/es6-promises/promises.d.ts +++ /dev/null @@ -1,39 +0,0 @@ -interface Thenable { - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Thenable; - then(onFulfill: (value: R) => U, onReject?: (error: any) => void): Thenable; - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => Thenable): Thenable; - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Thenable; - then(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable): Thenable; - then(onFulfill: (value: R) => U, onReject?: (error: any) => U): Thenable; -} - -declare class Promise implements Thenable { - constructor(callback: (resolve : (result: R) => void, reject: (error: any) => void) => void); - constructor(callback: (resolve : (result: Thenable) => void, reject: (error: any) => void) => void); - - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Promise; - then(onFulfill: (value: R) => U, onReject?: (error: any) => void): Promise; - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => Thenable): Promise; - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Promise; - then(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable): Promise; - then(onFulfill: (value: R) => U, onReject?: (error: any) => U): Promise; - - catch(onReject?: (error: any) => Thenable): Promise - catch(onReject?: (error: any) => U): Promise -} - -declare module Promise { - - - function cast(promise: Promise): Promise; - function cast(object: R): Promise; - - function resolve(thenable: Thenable): Promise; - function resolve(object: R): Promise; - - function reject(error: any): Promise; - - function all(promises: Promise[]): Promise; - - function race(promises: Promise[]): Promise; -} \ No newline at end of file From 4c956bc8d52a7f879077f2ebbfd451d8fa41dea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Campredon?= Date: Wed, 15 Jan 2014 18:09:00 +0100 Subject: [PATCH 3/7] correct references in test --- es6-promises/es6-promises-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/es6-promises/es6-promises-tests.ts b/es6-promises/es6-promises-tests.ts index fd1160572..5a8b81a35 100644 --- a/es6-promises/es6-promises-tests.ts +++ b/es6-promises/es6-promises-tests.ts @@ -1,4 +1,4 @@ -/// +/// var promiseString: Promise, promiseStringArr: Promise, From 62644f420e2f94221d666e8ea555e0e379dcf1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Campredon?= Date: Wed, 15 Jan 2014 18:11:12 +0100 Subject: [PATCH 4/7] add blank line --- es6-promises/es6-promises.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/es6-promises/es6-promises.d.ts b/es6-promises/es6-promises.d.ts index fc013817d..766fe3fbc 100644 --- a/es6-promises/es6-promises.d.ts +++ b/es6-promises/es6-promises.d.ts @@ -92,4 +92,4 @@ declare module Promise { * Make a Promise that fulfills when any item fulfills, and rejects if any item rejects. */ function race(promises: Promise[]): Promise; -} \ No newline at end of file +} From 620c86418298053cdc74ab23a78299c11c4e2987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Campredon?= Date: Thu, 16 Jan 2014 17:51:18 +0100 Subject: [PATCH 5/7] various defintion correction + more tests - correct onReject typings when onFullFill is 'undefined' - add onFullFill as optional parameter (to use myPromise.then()) - make cast/resolve/reject parameter optional - add examples from http://www.html5rocks.com/en/tutorials/es6/promises/ --- es6-promises/es6-promises-tests.ts | 93 +++++++++++++++++++++++- es6-promises/es6-promises.d.ts | 109 ++++++++++++++++++++++------- 2 files changed, 177 insertions(+), 25 deletions(-) diff --git a/es6-promises/es6-promises-tests.ts b/es6-promises/es6-promises-tests.ts index 5a8b81a35..3d4af2878 100644 --- a/es6-promises/es6-promises-tests.ts +++ b/es6-promises/es6-promises-tests.ts @@ -1,5 +1,6 @@ /// + var promiseString: Promise, promiseStringArr: Promise, arrayOfPromise: Promise[], @@ -14,7 +15,7 @@ var constructResult = new Promise((resolve, reject) => { promiseString = constructResult; -var constructResult1 = new Promise((resolve:(promise: Thenable) => void , reject) => { +var constructResult1 = new Promise((resolve:(promise: Thenable) => void) => { resolve(Promise.resolve('a string')); }); promiseString = constructResult1; @@ -70,9 +71,99 @@ promiseNumber = thenWithSimpleResultAndPromiseReject; var thenWithSimpleResultAndSimpleReject = promiseString.then(word => word.length, error => 10); promiseNumber = thenWithSimpleResultAndSimpleReject; +var thenWithUndefinedFullFillAndSimpleReject = promiseString.then(undefined, error => 10); +promiseNumber = thenWithUndefinedFullFillAndSimpleReject; + +var thenWithUndefinedFullFillAndPromiseReject = promiseString.then(undefined, error => Promise.resolve(10)); +promiseNumber = thenWithUndefinedFullFillAndPromiseReject; + +var thenWithNoResultAndNoReject = promiseString.then(); +promiseNumber = thenWithNoResultAndNoReject; + + //catch test var catchWithSimpleResult = promiseString.catch(error => 10); promiseNumber = catchWithSimpleResult; var catchWithPromiseResult = promiseString.catch(error => Promise.resolve(10)); promiseNumber = catchWithPromiseResult; + + +//examples coming from http://www.html5rocks.com/en/tutorials/es6/promises/ + +function get(url: string) { + // Return a new promise. + return new Promise(function(resolve, reject) { + // Do the usual XHR stuff + var req = new XMLHttpRequest(); + req.open('GET', url); + + req.onload = function() { + // This is called even on 404 etc + // so check the status + if (req.status == 200) { + // Resolve the promise with the response text + resolve(req.response); + } + else { + // Otherwise reject with the status text + // which will hopefully be a meaningful error + reject(Error(req.statusText)); + } + }; + + // Handle network errors + req.onerror = function() { + reject(Error("Network Error")); + }; + + // Make the request + req.send(); + }); +} + + + +function getJSON(url: string) { + return get(url).then(JSON.parse); +} + +function addHtmlToPage(html: string) { + +} + +function addTextToPage(text: string) { + +} + +interface Story { + heading: string; + chapterUrls: string[] +} + +getJSON('story.json').then(function(story: Story) { + addHtmlToPage(story.heading); + + // Map our array of chapter urls to + // an array of chapter json promises. + // This makes sure they all download parallel. + return story.chapterUrls.map(getJSON) + .reduce(function(sequence, chapterPromise) { + // Use reduce to chain the promises together, + // adding content to the page for each chapter + return sequence.then(function() { + // Wait for everything in the sequence so far, + // then wait for this chapter to arrive. + return chapterPromise; + }).then(function(chapter) { + addHtmlToPage(chapter.html); + }); + }, Promise.resolve()); +}).then(function() { + addTextToPage("All done"); +}).catch(function(err) { + // catch any error that happened along the way + addTextToPage("Argh, broken: " + err.message); +}).then(function() { + (document.querySelector('.spinner')).style.display = 'none'; +}); diff --git a/es6-promises/es6-promises.d.ts b/es6-promises/es6-promises.d.ts index 766fe3fbc..de651e42d 100644 --- a/es6-promises/es6-promises.d.ts +++ b/es6-promises/es6-promises.d.ts @@ -5,27 +5,78 @@ interface Thenable { - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Thenable; - then(onFulfill: (value: R) => U, onReject?: (error: any) => void): Thenable; - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => Thenable): Thenable; - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Thenable; - then(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable): Thenable; - then(onFulfill: (value: R) => U, onReject?: (error: any) => U): Thenable; + then(onFulfill: (value: R) => Thenable, onReject: (error: any) => Thenable): Thenable; + then(onFulfill: (value: R) => Thenable, onReject: (error: any) => U): Thenable; + then(onFulfill: (value: R) => U, onReject: (error: any) => Thenable): Thenable; + then(onFulfill: (value: R) => U, onReject: (error: any) => U): Thenable; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Thenable; + then(onFulfill?: (value: R) => U, onReject?: (error: any) => void): Thenable; + } declare class Promise implements Thenable { /** - * @param resolve Your promise is fulfilled with obj - * @rejectYour promise is rejected with obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error. Any errors thrown in the constructor callback will be implicitly passed to reject(). + * If you call resolve in the body of the callback passed to the constructor, + * your promise is fulfilled with result object passed to resolve. + * If you call reject your promise is rejected with the object passed to resolve. + * For consistency and debugging (eg stack traces), obj should be an instanceof Error. + * Any errors thrown in the constructor callback will be implicitly passed to reject(). */ constructor(callback: (resolve : (result: R) => void, reject: (error: any) => void) => void); /** - * @param resolve Your promise will be fulfilled/rejected with the outcome of thenable reject(obj) - * @rejectYour promise is rejected with obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error. Any errors thrown in the constructor callback will be implicitly passed to reject(). + * If you call resolve in the body of the callback passed to the constructor, + * your promise will be fulfilled/rejected with the outcome of thenable passed to resolve. + * If you call reject your promise is rejected with the object passed to resolve. + * For consistency and debugging (eg stack traces), obj should be an instanceof Error. + * Any errors thrown in the constructor callback will be implicitly passed to reject(). */ - constructor(callback: (resolve : (result: Thenable) => void, reject: (error: any) => void) => void); + constructor(callback: (resolve : (thenable: Thenable) => void, reject: (error: any) => void) => void); + /** + * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. + * Both callbacks have a single parameter , the fulfillment value or rejection reason. + * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. + * If an error is thrown in the callback, the returned promise rejects with that error. + * + * @param onFullFill called when/if "promise" resolves + * @param onReject called when/if "promise" rejects + */ + then(onFulfill: (value: R) => Thenable, onReject: (error: any) => Thenable): Promise; + /** + * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. + * Both callbacks have a single parameter , the fulfillment value or rejection reason. + * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. + * If an error is thrown in the callback, the returned promise rejects with that error. + * + * @param onFullFill called when/if "promise" resolves + * @param onReject called when/if "promise" rejects + */ + then(onFulfill: (value: R) => Thenable, onReject: (error: any) => U): Promise; + /** + * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. + * Both callbacks have a single parameter , the fulfillment value or rejection reason. + * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. + * If an error is thrown in the callback, the returned promise rejects with that error. + * + * @param onFullFill called when/if "promise" resolves + * @param onReject called when/if "promise" rejects + */ + then(onFulfill: (value: R) => U, onReject: (error: any) => Thenable): Promise; + /** + * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. + * Both callbacks have a single parameter , the fulfillment value or rejection reason. + * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. + * If an error is thrown in the callback, the returned promise rejects with that error. + * + * @param onFullFill called when/if "promise" resolves + * @param onReject called when/if "promise" rejects + */ + then(onFulfill: (value: R) => U, onReject: (error: any) => U): Promise; /** * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. @@ -37,20 +88,30 @@ declare class Promise implements Thenable { * @param onReject called when/if "promise" rejects */ then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Promise; - then(onFulfill: (value: R) => U, onReject?: (error: any) => void): Promise; - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => Thenable): Promise; - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Promise; - then(onFulfill: (value: R) => U, onReject?: (error: any) => Thenable): Promise; - then(onFulfill: (value: R) => U, onReject?: (error: any) => U): Promise; - + /** + * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. + * Both callbacks have a single parameter , the fulfillment value or rejection reason. + * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. + * If an error is thrown in the callback, the returned promise rejects with that error. + * + * @param onFullFill called when/if "promise" resolves + * @param onReject called when/if "promise" rejects + */ + then(onFulfill?: (value: R) => U, onReject?: (error: any) => void): Promise; /** * Sugar for promise.then(undefined, onRejected) * * @param onReject called when/if "promise" rejects */ - catch(onReject?: (error: any) => Thenable): Promise - catch(onReject?: (error: any) => U): Promise + catch(onReject?: (error: any) => Thenable): Promise; + /** + * Sugar for promise.then(undefined, onRejected) + * + * @param onReject called when/if "promise" rejects + */ + catch(onReject?: (error: any) => U): Promise; } declare module Promise { @@ -62,7 +123,7 @@ declare module Promise { /** * Make a promise that fulfills to obj. */ - function cast(object: R): Promise; + function cast(object?: R): Promise; /** @@ -74,16 +135,16 @@ declare module Promise { /** * Make a promise that fulfills to obj. Same as Promise.cast(obj) in this situation. */ - function resolve(object: R): Promise; + function resolve(object?: R): Promise; /** * Make a promise that rejects to obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error */ - function reject(error: any): Promise; + function reject(error?: any): Promise; /** * Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects. - * Eadd array item is passed to Promise.cast, so the array can be a mixture of promise-like objects and other objects. + * the array passed to all can be a mixture of promise-like objects and other objects. * The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value. */ function all(promises: Promise[]): Promise; @@ -92,4 +153,4 @@ declare module Promise { * Make a Promise that fulfills when any item fulfills, and rejects if any item rejects. */ function race(promises: Promise[]): Promise; -} +} \ No newline at end of file From 5b0c3efcc246b06345e2ec8e87f94e1a14919491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Campredon?= Date: Thu, 16 Jan 2014 18:19:53 +0100 Subject: [PATCH 6/7] "onFullFill" should read "onFulFill" --- es6-promises/es6-promises.d.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/es6-promises/es6-promises.d.ts b/es6-promises/es6-promises.d.ts index de651e42d..cec64a917 100644 --- a/es6-promises/es6-promises.d.ts +++ b/es6-promises/es6-promises.d.ts @@ -34,68 +34,68 @@ declare class Promise implements Thenable { /** - * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * onFulFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. * Both callbacks have a single parameter , the fulfillment value or rejection reason. * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. * If an error is thrown in the callback, the returned promise rejects with that error. * - * @param onFullFill called when/if "promise" resolves + * @param onFulFill called when/if "promise" resolves * @param onReject called when/if "promise" rejects */ then(onFulfill: (value: R) => Thenable, onReject: (error: any) => Thenable): Promise; /** - * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * onFulFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. * Both callbacks have a single parameter , the fulfillment value or rejection reason. * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. * If an error is thrown in the callback, the returned promise rejects with that error. * - * @param onFullFill called when/if "promise" resolves + * @param onFulFill called when/if "promise" resolves * @param onReject called when/if "promise" rejects */ then(onFulfill: (value: R) => Thenable, onReject: (error: any) => U): Promise; /** - * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * onFulFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. * Both callbacks have a single parameter , the fulfillment value or rejection reason. * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. * If an error is thrown in the callback, the returned promise rejects with that error. * - * @param onFullFill called when/if "promise" resolves + * @param onFulFill called when/if "promise" resolves * @param onReject called when/if "promise" rejects */ then(onFulfill: (value: R) => U, onReject: (error: any) => Thenable): Promise; /** - * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * onFulFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. * Both callbacks have a single parameter , the fulfillment value or rejection reason. * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. * If an error is thrown in the callback, the returned promise rejects with that error. * - * @param onFullFill called when/if "promise" resolves + * @param onFulFill called when/if "promise" resolves * @param onReject called when/if "promise" rejects */ then(onFulfill: (value: R) => U, onReject: (error: any) => U): Promise; /** - * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * onFulFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. * Both callbacks have a single parameter , the fulfillment value or rejection reason. * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. * If an error is thrown in the callback, the returned promise rejects with that error. * - * @param onFullFill called when/if "promise" resolves + * @param onFulFill called when/if "promise" resolves * @param onReject called when/if "promise" rejects */ then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Promise; /** - * onFullFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. + * onFulFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. * Both callbacks have a single parameter , the fulfillment value or rejection reason. * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. * If an error is thrown in the callback, the returned promise rejects with that error. * - * @param onFullFill called when/if "promise" resolves + * @param onFulFill called when/if "promise" resolves * @param onReject called when/if "promise" rejects */ then(onFulfill?: (value: R) => U, onReject?: (error: any) => void): Promise; From 9b6ca433a2058cd3493ce2afeca2d87baeb71f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Campredon?= Date: Sat, 18 Jan 2014 12:01:35 +0100 Subject: [PATCH 7/7] remove void onReject oveloads --- es6-promises/es6-promises-tests.ts | 9 +------- es6-promises/es6-promises.d.ts | 33 +++++------------------------- 2 files changed, 6 insertions(+), 36 deletions(-) diff --git a/es6-promises/es6-promises-tests.ts b/es6-promises/es6-promises-tests.ts index 3d4af2878..774d3763e 100644 --- a/es6-promises/es6-promises-tests.ts +++ b/es6-promises/es6-promises-tests.ts @@ -44,15 +44,12 @@ 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; @@ -62,9 +59,6 @@ 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; @@ -80,7 +74,6 @@ promiseNumber = thenWithUndefinedFullFillAndPromiseReject; var thenWithNoResultAndNoReject = promiseString.then(); promiseNumber = thenWithNoResultAndNoReject; - //catch test var catchWithSimpleResult = promiseString.catch(error => 10); promiseNumber = catchWithSimpleResult; diff --git a/es6-promises/es6-promises.d.ts b/es6-promises/es6-promises.d.ts index cec64a917..a44355233 100644 --- a/es6-promises/es6-promises.d.ts +++ b/es6-promises/es6-promises.d.ts @@ -6,11 +6,9 @@ interface Thenable { then(onFulfill: (value: R) => Thenable, onReject: (error: any) => Thenable): Thenable; - then(onFulfill: (value: R) => Thenable, onReject: (error: any) => U): Thenable; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Thenable; then(onFulfill: (value: R) => U, onReject: (error: any) => Thenable): Thenable; - then(onFulfill: (value: R) => U, onReject: (error: any) => U): Thenable; - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Thenable; - then(onFulfill?: (value: R) => U, onReject?: (error: any) => void): Thenable; + then(onFulfill?: (value: R) => U, onReject?: (error: any) => U): Thenable; } @@ -54,7 +52,7 @@ declare class Promise implements Thenable { * @param onFulFill called when/if "promise" resolves * @param onReject called when/if "promise" rejects */ - then(onFulfill: (value: R) => Thenable, onReject: (error: any) => U): Promise; + then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => U): Promise; /** * onFulFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. @@ -76,29 +74,8 @@ declare class Promise implements Thenable { * @param onFulFill called when/if "promise" resolves * @param onReject called when/if "promise" rejects */ - then(onFulfill: (value: R) => U, onReject: (error: any) => U): Promise; - /** - * onFulFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. - * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. - * Both callbacks have a single parameter , the fulfillment value or rejection reason. - * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. - * If an error is thrown in the callback, the returned promise rejects with that error. - * - * @param onFulFill called when/if "promise" resolves - * @param onReject called when/if "promise" rejects - */ - then(onFulfill: (value: R) => Thenable, onReject?: (error: any) => void): Promise; - /** - * onFulFill is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. - * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. - * Both callbacks have a single parameter , the fulfillment value or rejection reason. - * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. - * If an error is thrown in the callback, the returned promise rejects with that error. - * - * @param onFulFill called when/if "promise" resolves - * @param onReject called when/if "promise" rejects - */ - then(onFulfill?: (value: R) => U, onReject?: (error: any) => void): Promise; + then(onFulfill?: (value: R) => U, onReject?: (error: any) => U): Promise; + /** * Sugar for promise.then(undefined, onRejected)