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