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