123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>JSDoc: Source: polyfill/Promise.js</title>
- <script src="scripts/prettify/prettify.js"> </script>
- <script src="scripts/prettify/lang-css.js"> </script>
- <!--[if lt IE 9]>
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
- <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
- </head>
- <body>
- <div id="main">
- <h1 class="page-title">Source: polyfill/Promise.js</h1>
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>// Licensed Materials - Property of IBM
- //
- // IBM Watson Analytics
- //
- // (C) Copyright IBM Corp. 2015
- //
- // US Government Users Restricted Rights - Use, duplication or
- // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- module.exports = ( function(
- ObjectPolyfill,
- setImmediate
- )
- {
- "use strict";
- /*jshint latedef:false*/
- /**
- * Helper method that turns an _iterable into an array of promises
- * @memberof module:barejs/polyfill.Promise~
- * @private
- */
- function iterableToPromiseArray( _iterable )
- {
- var promises;
- var it;
- var value;
- if ( _iterable.length || _iterable.length === 0 )
- {
- promises = new Array( _iterable.length );
- for ( var i = 0, len = _iterable.length; i < len; ++i )
- {
- value = _iterable[i];
- if ( ( !value ) || ( typeof value.then !== "function" ) )
- value = Promise.resolve( value );
- promises[i] = value;
- }
- }
- else if ( ( it = ObjectPolyfill.getIterator( _iterable ) ) )
- {
- promises = [];
- for( value = it.next(); !value.done; value = it.next() )
- {
- if ( ( !value.value ) || ( typeof value.value.then !== "function" ) )
- promises.push( Promise.resolve( value.value ) );
- else
- promises.push( value.value );
- }
- }
- else
- {
- throw new Error( "Invalid iterable" );
- }
- return promises;
- }
- /**
- * Handler for a promise
- * @class module:barejs/polyfill.Promise~Handler
- * @private
- */
- function Handler( _onFulfilled, _onRejected, _promise )
- {
- this.onFulfilled = typeof _onFulfilled === "function" ? _onFulfilled : null;
- this.onRejected = typeof _onRejected === "function" ? _onRejected : null;
- this.promise = _promise;
- }
- /**
- * No-operation function
- */
- function noop() {}
- // States:
- //
- // 0 - pending
- // 1 - fulfilled with _value
- // 2 - rejected with _value
- // 3 - adopted the state of another promise, _value
- //
- // once the state is no longer pending (0) it is immutable
- // All `_` prefixed properties will be reduced to `_{random number}`
- // at build time to obfuscate them and discourage their use.
- // We don't use symbols or Object.defineProperty to fully hide them
- // because the performance isn't good enough.
- // to avoid using try/catch inside critical functions, we
- // extract them to here.
- var LAST_ERROR = null;
- var IS_ERROR = {};
- /**
- * Safely request the then function from an object
- */
- function getThen( _obj )
- {
- try
- {
- return _obj.then;
- }
- catch ( ex )
- {
- LAST_ERROR = ex;
- return IS_ERROR;
- }
- }
- function tryCallOne( _fn, _a )
- {
- try
- {
- return _fn( _a );
- }
- catch ( ex )
- {
- LAST_ERROR = ex;
- return IS_ERROR;
- }
- }
- function tryCallTwo( _fn, _a, _b )
- {
- try
- {
- _fn( _a, _b );
- }
- catch ( ex )
- {
- LAST_ERROR = ex;
- return IS_ERROR;
- }
- }
- /**
- * Create a new Promise
- * @class module:barejs/polyfill.Promise
- * @param {function} _resolver The resolver function that will be called with two callbacks:
- * _resolve (call on succes) and _reject (call on failure).
- */
- function Promise( _resolver )
- {
- if ( typeof this !== "object" )
- throw new TypeError( "Promises must be constructed via new" );
- this._state = 0;
- this._value = null;
- this._deferreds = [];
- if ( ObjectPolyfill.ensureCallable( _resolver ) === noop )
- return;
- doResolve( _resolver, this );
- }
- function safeThen( _self, _onFulfilled, _onRejected )
- {
- return new _self.constructor( function( resolve, reject )
- {
- var res = new Promise( noop );
- res.then( resolve, reject );
- handle( _self, new Handler( _onFulfilled, _onRejected, res ) );
- } );
- }
- function handle( _self, _deferred )
- {
- while ( _self._state === 3 )
- {
- _self = _self._value;
- }
- if ( _self._state === 0 )
- {
- _self._deferreds.push( _deferred );
- return;
- }
- setImmediate( function()
- {
- var cb = _self._state === 1 ? _deferred.onFulfilled : _deferred.onRejected;
- if ( cb === null )
- {
- if ( _self._state === 1 )
- {
- resolve( _deferred.promise, _self._value );
- }
- else
- {
- reject( _deferred.promise, _self._value );
- }
- return;
- }
- var ret = tryCallOne( cb, _self._value );
- if ( ret === IS_ERROR )
- {
- reject( _deferred.promise, LAST_ERROR );
- }
- else
- {
- resolve( _deferred.promise, ret );
- }
- } );
- }
- function resolve( _self, _newValue )
- {
- // Promise Resolution Procedure:
- // https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
- if ( _newValue === _self )
- {
- return reject( _self, new TypeError( 'A promise cannot be resolved with itself.' ) );
- }
- if ( _newValue && (typeof _newValue === "object" || typeof _newValue === "function") )
- {
- var then = getThen( _newValue );
- if ( then === IS_ERROR )
- {
- return reject( _self, LAST_ERROR );
- }
- if ( then === _self.then && ( _newValue instanceof Promise ) )
- {
- _self._state = 3;
- _self._value = _newValue;
- finale( _self );
- return;
- }
- else if ( typeof then === "function" )
- {
- doResolve( then.bind( _newValue ), _self );
- return;
- }
- }
- _self._state = 1;
- _self._value = _newValue;
- finale( _self );
- }
- function reject( _self, _newValue )
- {
- _self._state = 2;
- _self._value = _newValue;
- finale( _self );
- }
- function finale( _self )
- {
- for ( var i = 0; i < _self._deferreds.length; ++i )
- handle( _self, _self._deferreds[ i ] );
- _self._deferreds = null;
- }
- /**
- * Take a potentially misbehaving resolver function and make sure onFulfilled and onRejected
- * are only called once.
- *
- * Makes no guarantees about asynchrony.
- * @memberof module:barejs/polyfill.Promise~
- * @private
- */
- function doResolve( _resolver, _promise )
- {
- var done = false;
- var res = tryCallTwo( _resolver, function( value )
- {
- if ( done )
- return;
- done = true;
- resolve( _promise, value );
- }, function( reason )
- {
- if ( done )
- return;
- done = true;
- reject( _promise, reason );
- } );
- if ( !done && res === IS_ERROR )
- {
- done = true;
- reject( _promise, LAST_ERROR );
- }
- }
- return ObjectPolyfill.polyfill( Promise,
- /** @lends module:barejs/polyfill.Promise */
- {
- _noop: noop,
- /**
- * The Promise.resolve( _value ) method returns a Promise object that is resolved with the given value.
- * If the value is a thenable (i.e. has a then method), the returned promise will "follow" that thenable,
- * adopting its eventual state; otherwise the returned promise will be fulfilled with the value.
- * @param {*} _value The value to resolve with.
- * @returns {module:barejs/polyfill.Promise} A Promise that is resolved with _value.
- */
- resolve: function resolve( _value )
- {
- return new Promise( function( _resolve, _reject )
- {
- _resolve( _value );
- } );
- },
- /**
- * The Promise.reject( _reason ) method returns a Promise object that is rejected with the given reason.
- * @param {*} _reason The rejection reason (passed as rejection argument).
- * @returns {module:barejs/polyfill.Promise} A Promise that is rejected with _reason.
- */
- reject: function reject( _reason )
- {
- return new Promise( function( _resolve, _reject )
- {
- _reject( _reason );
- } );
- },
- /**
- * The Promise.all( _iterable ) method returns a promise that resolves when all of the promises
- * in the iterable argument have resolved. If any of the passed in promises rejects, the all
- * Promise immediately rejects with the value of the promise that rejected, discarding all the
- * other promises whether or not they have resolved.
- * @param _iterable {Object} Array that can be iterated.
- * @returns {module:barejs/polyfill.Promise} A promise that will resolve with an array of values corresponding to all Promises in _iterable, after every Promise is resolved.
- */
- all: function all( _iterable )
- {
- var promises = iterableToPromiseArray( _iterable );
- if ( promises.length < 1 )
- return Promise.resolve( promises );
- return new Promise( function( _resolve, _reject )
- {
- var values = new Array( promises.length );
- var resolveCount = 0;
- function rejected( _reason )
- {
- if ( values )
- {
- _reject( _reason );
- values = null;
- }
- }
- function resolved( _index, _value )
- {
- if ( values )
- {
- // Promises should never resolve twice, so we don't perform any sanity checking
- // to see if values.hasOwnProperty( _index )...
- values[_index] = _value;
- if ( ++resolveCount >= values.length )
- {
- _resolve( values );
- values = null;
- }
- }
- }
- for ( var i = 0, len = promises.length; i < len; ++i )
- promises[i].then( resolved.bind( null, i ), rejected );
- } );
- },
- /**
- * The Promise.race( _iterable ) method returns a promise that resolves or rejects as soon as one
- * of the promises in the iterable resolves or rejects, with the value or reason from that promise.
- * @returns {module:barejs/polyfill.Promise} A promise that will resolve with the value of the first Promise to resolve.
- */
- race: function race( _iterable )
- {
- var promises = iterableToPromiseArray( _iterable );
- return new Promise( function( _resolve, _reject )
- {
- var isResolved = false;
- function rejected( _reason )
- {
- if ( !isResolved )
- {
- _reject( _reason );
- isResolved = true;
- }
- }
- function resolved( _value )
- {
- if ( !isResolved )
- {
- _resolve( _value );
- isResolved = true;
- }
- }
- for ( var i = 0, len = promises.length; i < len; ++i )
- promises[i].then( resolved, rejected );
- } );
- }
- },
- /** @lends module:barejs/polyfill.Promise# */
- {
- /**
- * Register either a resolve or reject callback, or both.
- * @returns {module:barejs/polyfill.Promise} A promise that will resolve or reject with the value returned by (or thrown from) _onFulfilled or _onRejected.
- */
- then: function( _onFulfilled, _onRejected )
- {
- if ( this.constructor !== Promise )
- return safeThen( this, _onFulfilled, _onRejected );
- var res = new Promise( noop );
- handle( this, new Handler( _onFulfilled, _onRejected, res ) );
- return res;
- },
- /**
- * Register a rejection callback (shortcut for then( null, _onRejected ) ).
- * @returns {module:barejs/polyfill.Promise} A promise that will resolve or reject with the value returned by (or thrown from) _onRejected.
- */
- "catch": function( _onRejected )
- {
- return this.then( null, _onRejected );
- }
- }, null, "Promise" );
- }(
- require( "./Object" ),
- /* global setImmediate, setTimeout */
- typeof setImmediate !== "undefined" ? setImmediate : setTimeout
- ) );
- </code></pre>
- </article>
- </section>
- </div>
- <nav>
- <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-barejs.html">barejs</a></li><li><a href="module-barejs_polyfill.html">barejs/polyfill</a></li><li><a href="module-barejs_polyfill_Intl.html">barejs/polyfill/Intl</a></li></ul><h3>Classes</h3><ul><li><a href="module-barejs.decl.html">decl</a></li><li><a href="module-barejs.decl-Enum.html">Enum</a></li><li><a href="module-barejs.decl-Interface.html">Interface</a></li><li><a href="module-barejs.decl-SpecialType.html">SpecialType</a></li><li><a href="module-barejs.Destroyable.html">Destroyable</a></li><li><a href="module-barejs.EventArgs.html">EventArgs</a></li><li><a href="module-barejs.Evented.html">Evented</a></li><li><a href="module-barejs.Evented-EventedHandle.html">EventedHandle</a></li><li><a href="module-barejs.Exception.html">Exception</a></li><li><a href="module-barejs_polyfill.Array.html">Array</a></li><li><a href="module-barejs_polyfill.Date.html">Date</a></li><li><a href="module-barejs_polyfill.EntryStore.html">EntryStore</a></li><li><a href="module-barejs_polyfill.EntryStore.Iterator.html">Iterator</a></li><li><a href="module-barejs_polyfill.Function.html">Function</a></li><li><a href="module-barejs_polyfill.Map.html">Map</a></li><li><a href="module-barejs_polyfill.Map-MapIterator.html">MapIterator</a></li><li><a href="module-barejs_polyfill.Math.html">Math</a></li><li><a href="module-barejs_polyfill.Number.html">Number</a></li><li><a href="module-barejs_polyfill.Object.html">Object</a></li><li><a href="module-barejs_polyfill.Promise.html">Promise</a></li><li><a href="module-barejs_polyfill.Set.html">Set</a></li><li><a href="module-barejs_polyfill.Set-SetIterator.html">SetIterator</a></li><li><a href="module-barejs_polyfill.String.html">String</a></li><li><a href="module-barejs_polyfill.Symbol.html">Symbol</a></li><li><a href="module-barejs_polyfill.WeakMap.html">WeakMap</a></li><li><a href="module-barejs_polyfill.WeakSet.html">WeakSet</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat.html">DateTimeFormat</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat-DateTimeFormatOptions.html">DateTimeFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat.html">NumberFormat</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat-NumberFormatOptions.html">NumberFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl-Format.html">Format</a></li></ul>
- </nav>
- <br class="clear">
- <footer>
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Oct 03 2018 15:59:34 GMT+0200 (W. Europe Daylight Time)
- </footer>
- <script> prettyPrint(); </script>
- <script src="scripts/linenumber.js"> </script>
- </body>
- </html>
|