163 lines
3.0 KiB
HTML
163 lines
3.0 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: modules/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: modules/promise.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>/**
|
|
* Module for polyfilling promises
|
|
*/
|
|
(function(w, undefined) {
|
|
|
|
if (typeof Promise === "undefined")
|
|
{
|
|
/**
|
|
* Promise constructor
|
|
*
|
|
* @constructor
|
|
* @namespace
|
|
* @param function fn
|
|
*/
|
|
w.Promise = function(fn)
|
|
{
|
|
var state = 'pending';
|
|
var value;
|
|
var deferred = null;
|
|
|
|
function resolve(newValue)
|
|
{
|
|
try
|
|
{
|
|
if (newValue && typeof newValue.then === 'function')
|
|
{
|
|
newValue.then(resolve);
|
|
return;
|
|
}
|
|
|
|
state = 'resolved';
|
|
value = newValue;
|
|
|
|
if (deferred)
|
|
{
|
|
handle(deferred);
|
|
}
|
|
}
|
|
catch(e)
|
|
{
|
|
reject(e);
|
|
}
|
|
}
|
|
|
|
function reject(reason)
|
|
{
|
|
state = 'rejected';
|
|
value = reason;
|
|
|
|
if (deferred)
|
|
{
|
|
handle(deferred);
|
|
}
|
|
|
|
}
|
|
|
|
function handle(handler)
|
|
{
|
|
if (state === 'pending')
|
|
{
|
|
deferred = onResolved;
|
|
return;
|
|
}
|
|
|
|
var handlerCallback;
|
|
|
|
setTimeout(function() {
|
|
handlerCallback = (state === 'resolved')
|
|
? handler.onResolved
|
|
: handler.onRejected;
|
|
|
|
|
|
if ( ! handlerCallback)
|
|
{
|
|
|
|
(state === 'resolved')
|
|
? handler.resolve(value)
|
|
: handler.reject(value);
|
|
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
ret = handlerCallback(value);
|
|
}
|
|
catch(e)
|
|
{
|
|
handler.reject(e);
|
|
return;
|
|
}
|
|
|
|
handler.resolve(ret);
|
|
}, 1);
|
|
}
|
|
|
|
this.then = function(onResolved, onRejected) {
|
|
return new Promise(function(resolve, reject) {
|
|
handle({
|
|
onResolved: onResolved,
|
|
onRejected: onRejected,
|
|
resolve: resolve,
|
|
reject: reject
|
|
});
|
|
});
|
|
};
|
|
|
|
fn(resolve, reject);
|
|
}
|
|
}
|
|
|
|
})(window);
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Index</a></h2><h3>Namespaces</h3><ul><li><a href="$_.html">$_</a></li><li><a href="$_.dom.html">dom</a></li><li><a href="$_.event.html">event</a></li><li><a href="$_.store.html">store</a></li><li><a href="w.Promise.html">Promise</a></li></ul><h3>Global</h3><ul><li><a href="global.html#data[undefined]">data[undefined]</a></li></ul>
|
|
</nav>
|
|
|
|
<br clear="both">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a> on Fri Sep 05 2014 11:37:54 GMT-0400 (EDT)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|