var _manager function var deferred defer _doer then function data cons

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var _manager = function (i) {
var deferred = $q.defer();
_doer(i)
.then(function (data) {
i++;
console.log('isStop = ' + isStop, 'i = ' + i);
if (data.state === 'ok' && !isStop && i < 7) {
//deferred.resolve({state: 'ok', i: i});
return _manager(i);
} else {
//deferred.resolve({state: 'end', i: data.i});
/* в следующий then передаем data через return */
return {state: 'end', i: data.i};
}
}, function (data) {
//deferred.reject({state: 'error', i: data.i});
return {state: 'error', i: data.i};
})
.then(function (data) {
if(data.state === 'ok') {
deferred.resolve({state: 'ok', i: data.i});
} else if (data.state === 'end') {
deferred.resolve({state: 'end', i: data.i});
} else {
deferred.resolve({state: 'error', i: data.i});
}
})
;
return deferred.promise;
};