/*
	Copyright (c) 2004-2016, The JS Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/

//>>built
define("dojo/Deferred",["./has","./_base/lang","./errors/CancelError","./promise/Promise","./promise/instrumentation"],function(B,C,x,y,u){var z=Object.freeze||function(){},r=function(c,a,d,b,f){2===a&&h.instrumentRejected&&0===c.length&&h.instrumentRejected(d,!1,b,f);for(f=0;f<c.length;f++)v(c[f],a,d,b)},v=function(c,a,d,b){var f=c[a],g=c.deferred;if(f)try{var k=f(d);if(0===a)"undefined"!==typeof k&&p(g,a,k);else{if(k&&"function"===typeof k.then){c.cancel=k.cancel;k.then(t(g,1),t(g,2),t(g,0));return}p(g,
1,k)}}catch(l){p(g,2,l)}else p(g,a,d);2===a&&h.instrumentRejected&&h.instrumentRejected(d,!!f,b,g.promise)},t=function(c,a){return function(d){p(c,a,d)}},p=function(c,a,d){if(!c.isCanceled())switch(a){case 0:c.progress(d);break;case 1:c.resolve(d);break;case 2:c.reject(d)}},h=function(c){var a=this.promise=new y,d=this,b,f,g,k=!1,l=[];Error.captureStackTrace&&(Error.captureStackTrace(d,h),Error.captureStackTrace(a,h));this.isResolved=a.isResolved=function(){return 1===b};this.isRejected=a.isRejected=
function(){return 2===b};this.isFulfilled=a.isFulfilled=function(){return!!b};this.isCanceled=a.isCanceled=function(){return k};this.progress=function(e,m){if(b){if(!0===m)throw Error("This deferred has already been fulfilled.");return a}r(l,0,e,null,d);return a};this.resolve=function(e,m){if(b){if(!0===m)throw Error("This deferred has already been fulfilled.");return a}r(l,b=1,f=e,null,d);l=null;return a};var w=this.reject=function(e,m){if(b){if(!0===m)throw Error("This deferred has already been fulfilled.");
return a}Error.captureStackTrace&&Error.captureStackTrace(g={},w);r(l,b=2,f=e,g,d);l=null;return a};this.then=a.then=function(e,m,q){var n=[q,e,m];n.cancel=a.cancel;n.deferred=new h(function(A){return n.cancel&&n.cancel(A)});b&&!l?v(n,b,f,g):l.push(n);return n.deferred.promise};this.cancel=a.cancel=function(e,m){if(!b){if(c){var q=c(e);e="undefined"===typeof q?e:q}k=!0;if(!b)return"undefined"===typeof e&&(e=new x),w(e),e;if(2===b&&f===e)return e}else if(!0===m)throw Error("This deferred has already been fulfilled.");
};z(a)};h.prototype.toString=function(){return"[object Deferred]"};u&&u(h);return h});