(function(k){var c=function(e){var a=function(){var a,e,f=[];f.notify=function(b){f.forEach(function(a){a(b)})};var d=[];d._push=d.push;d.push=function(){d._push.apply(d,arguments);d.check()};d.check=function(){if(void 0!==a)for(var b;d.length;)if(b=d.shift(),void 0===b.state||b.state===a)e=("function"===typeof b.f?b.f.call(g,e):b.f)||e};var g={promise:function(){return g},done:function(b){d.push({state:!0,f:b});return g},fail:function(b){d.push({state:!1,f:b});return g},always:function(b){d.push({f:b});
return g},progress:function(b){f.push(b);return g},then:function(b,a,d){return c(function(c){[{fn:"done",forward:"resolve",f:b},{fn:"fail",forward:"reject",f:a},{fn:"progress",forward:"notify",f:d}].forEach(function(b){g[b.fn](function(){var a="function"===typeof b.f,d=a?b.f(e):void 0;d&&"function"===typeof d.promise?d.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[b.forward].apply(c,a?[d]:arguments)})})}).promise()},each:function(b){return g.then(function(a){a instanceof Array||(a=
arguments);return c.when(a.map(function(a){return b(a)}))})},iterate:function(a){return g.then(function(d){d instanceof Array||(d=arguments);var e=0,f=c(),g=function(){if(e<d.length){var c=d[e++];(c=a(c))&&c.promise?c.promise().done(g).fail(f.reject):g()}else f.resolve(d)};g();return f.promise()})}};return{get:function(){return g},try_resolve:function(b){void 0===a&&(a=!0,e=b);d.check()},try_reject:function(b){void 0===a&&(a=!1,e=b);d.check()},do_notify:function(a){f.notify(a)}}}(),f={promise:function(){return a.get()},
resolve:function(){return a.try_resolve.apply(this,arguments)},reject:function(){return a.try_reject.apply(this,arguments)},notify:function(){return a.do_notify.apply(this,arguments)}};e&&e(f);return f};c.Pledge=function(e){var a=c();a.resolve(e);return a.promise()};c.Breach=function(e){var a=c();a.reject(e);return a.promise()};c.when=function(e){e instanceof Array||(e=arguments);var a=c(),f=e.length,h=[];f?e.forEach(function(c){c&&c.promise&&c.promise().fail(function(){a.reject(h);f=-1}).done(function(c){h.push(c);
0===--f&&a.resolve(h)})}):a.resolve(h);return a.promise()};k.Registry?k.Registry.register("promise","0",function(){return c}):"undefined"!==typeof require?module.exports.Deferred=c:k.Deferred=c})("undefined"!==typeof window?window:GLOBAL);
