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