pledges
Version:
A micro JS library for promises based on the Promises/A+ specification.
11 lines • 1 kB
JavaScript
/*!
* pledges.
* https://www.github.com/ryansmith94/Pledges
*
* Copyright 2013, ryansmith94 and contributors.
* Released under the MIT license.
* https://github.com/ryansmith94/Pledges/blob/master/license.md
*
* Version 3.0.0.
*/
!function(a){"use strict";var b=function(){var a=this,b=0,d=null,e=[],f=[],g=function(c,e){return function(f){return 0===b&&(d=f,b=c,e.forEach(function(a){a()})),a}};return a.state=function(){var a=["pending","fulfilled","rejected"];return function(){return a[b]}}(),a.resolve=g(1,e),a.reject=g(2,f),a.then=function(a,g){var h=c(),i=function(a,b){return"function"!=typeof a&&(a=b),function(){setTimeout(function(){try{h.resolve(a(d))}catch(b){h.reject(b)}})}};return a=i(a,function(a){return a}),g=i(g,function(a){throw a}),0===b?(e.push(a),f.push(g)):1===b?a():2===b&&g(),h.restrict()},a.restrict=function(){return{state:a.state,then:a.then}},a},c=function(){return new b};"function"==typeof a.define?a.define("deferred",[],function(){return c}):a.deferred=c}(this);