\$deferred.done(function() { // Condition \$deferred.done(function() { // Condition \$deferred.done(function() { // Condition \$deferred.done(function() { // Condition \$deferred.done(function() { // Condition // do something }); }); }); }); });

 var _once = true; function finalCheck() { if(!_once) return; _once = false;   \$deferred.done(function() { // Condition // do something }); }   \$deferred.done(function() { // Condition \$deferred.done(function() { // Condition \$deferred.done(function() { // Condition finalCheck(); }); \$deferred.done(function() { // Condition \$deferred.done(function() { // Condition finalCheck(); }); }); }); });

 第一种结构设计： new Condition(function(result) { // Condition result.success(); }).next(function(result) { // Condition result.success(); }).next(function(result) { // Condition result.success(); }).next(function(result) { // Condition result.success(); }).next(function(result) { // Condition result.success(); }). success(function() { // do something }).start(); 第二种结构设计： var seq1 = new Condition(function(result) { // Condition result.success(); }).next(function(result) { // Condition result.success(); });   var seq2_1 = new Condition(function(result) { // Condition result.success(); }); var seq2_2 = new Condition(function(result) { // Condition result.success() }).next(function(result) { // Condition result.success(); });   seq1 .nextOr(seq2_1, seq2_2) .next(function(result) { // Condition result.success(); }). success(function() { // do something }).start();

 var Condition = function(func) { this.func = func; this.root = null; this.successFunc = null; return this; };

 Condition.prototype.next = function(func) { var _next = new Condition(func); _next.root = this.root || this; this._next = _next; return _next; };

 Condition.prototype.success = function(func) { this.successFunc = func; return this; };

 Condition.prototype._doAction = function() { var _this = this; _this.func({ success: function() { // 如果该组件有success回调，则调用 if(_this.successFunc) { _this.successFunc(); } // 如果有后续组件则继续调用 if(_this._next) { _this._next._doAction(); } } }); };   Condition.prototype.start = function() { (this.root || this)._doAction(); };

 new Condition(function(result) { console.log(Condition 1); result.success(); }).success(function() { console.log(Success 1); }).next(function(result) { console.log(Condition 2); result.success(); }).success(function() { console.log(Success 2); }).start();   // Condition 1 // Success 1 // Condition 2 // Success 2

 var Condition = function(func) { this.func = func; this.root = null; this.successFunc = []; return this; }; Condition.prototype.success = function(func) { this.successFunc.push(func); return this; }; // 如果该组件有success回调，则调用 for(var i = 0 ; i < _this.successFunc.length ; i += 1) { _this.successFunc[i](); }

 Condition.prototype.nextOr = function() { var _next = new Condition(function(result) { result.success(); }); _next.root = this.root || this; this._next = _next;   this._nextList = arguments; for(var i = 0 ; i < arguments.length ; i += 1) { arguments[i].success(function() { _next._doAction(); }); } return _next; };

 Condition.prototype._doAction = function() { var _this = this; _this.func({ success: function() { // 如果该组件有success回调，则调用 if(_this.successFunc) { _this.successFunc(); } // 如果有后续组件则继续调用 if(_this._next) { if(_this._nextList) { for(var i = 0 ; i < _this._nextList.length ; i += 1) { _this._nextList[i].start (); } } else { _this._next._doAction(); } } } }); };

 var s1 = new Condition(function(result) { console.log(Condition 1.1); result.success(); }).next(function(result) { console.log(Condition 1.2); result.success(); });   var s2_1 = new Condition(function(result) { console.log(Condition 2-1); result.success(); }); var s2_2 = new Condition(function(result) { console.log(Condition 2-2.1); result.success(); }).next(function(result) { console.log(Condition 2-2.2); result.success(); }); s1.nextOr(s2_1, s2_2).success(function() { console.log(Condition 3); }).start();   // Condition 1.1 // Condition 1.2 // Condition 2-1 // Condition 3 // Condition 2-2.1 // Condition 2-2.2 // Condition 3

 Condition.prototype.nextOr = function() { var _once = true; var _next = new Condition(function(result) { result.success(); }); _next.root = this.root || this; this._next = _next;   this._nextList = arguments; for(var i = 0 ; i < arguments.length ; i += 1) { arguments[i].success(function() { if(!_once) return; _once = false;   _next._doAction(); }); } return _next; };

 // Condition 1.1 // Condition 1.2 // Condition 2-1 // Condition 3 // Condition 2-2.1 // Condition 2-2.2

 小贴士：在Condition中使用once和原来的结构中使用once区别是什么？ 将分支管理逻辑从代码中剥离可以减少代码复杂度，从而提升代码的可读性和维护性。

 Condition.prototype._doAction = function() { var _succeed = false; var _this = this; var i;   _this.func({ success: function() { // 如果该组件有success回调，则调用 for(i = 0 ; i < _this.successFunc.length ; i += 1) { _this.successFunc[i](); } // 如果有后续组件则继续调用 if(_this._next) { if(_this._nextList) { for(i = 0 ; i < _this._nextList.length ; i += 1) { if(_succeed) break;   _this._nextList[i].success(function() { _succeed = true; }).start(); } } else { _this._next._doAction(); } } } }); };

 // Condition 1.1 // Condition 1.2 // Condition 2-1 // Condition 3

 var s1 = new Condition(function(result) { console.log(Condition 1.1); result.success(); }).next(function(result) { console.log(Condition 1.2); result.success(); });   var s2_1 = new Condition(function(result) { console.log(Condition 2-1); setTimeout(function() { result.success(); }, 100); }); var s2_2 = new Condition(function(result) { console.log(Condition 2-2.1); setTimeout(function() { result.success(); }, 100); }).next(function(result) { console.log(Condition 2-2.2); setTimeout(function() { result.success(); }, 100); }); s1.nextOr(s2_1, s2_2).success(function() { console.log(Condition 3); }).start();   // Condition 1.1 // Condition 1.2 // Condition 2-1 // Condition 2-2.1 // Condition 3 // Condition 2-2.2

 var Condition = function(func) { this.func = func; this.root = null; this.successFunc = []; this._stop = false; return this; }; Condition.prototype._doAction = function() { var _succeed = false; var _this = this; var i; if((this.root || this)._stop) return; … }; Condition.prototype.start = function() { (this.root || this)._stop = false; (this.root || this)._doAction(); }; Condition.prototype.nextOr = function() { var _once = true; var _this = this; var i;   var _next = new Condition(function(result) { result.success(); }); _next.root = _this.root || _this; _this._next = _next;   _this._nextList = arguments; function doSuccess() { for(i = 0 ; i < _this._nextList.length ; i += 1) { var _root = _this._nextList[i]; _root = _root.root || _root; _root._stop = true; }   if(!_once) return; _once = false;   _next._doAction(); } for(i = 0 ; i < arguments.length ; i += 1) { arguments[i].success(function() { doSuccess(); }); } return _next; };

 // Condition 1.1 // Condition 1.2 // Condition 2-1 // Condition 2-2.1 // Condition 3