jseppi/angular-queue — pull request #3 by beardedlinuxgeek (2014-07-31 23:01:02+00:00)
Title: pause() doesn't work from within a callback

I added this to the pause function
```javascript
console.log("PAUSE");
```
And I changed myCallback() from the example to
```javascript
var myCallback = function(item) {
console.log(item);
myQueue.pause();
};
```

My output was
```
item 1
PAUSE
item 2
PAUSE
item 3
PAUSE
complete!
```
so clearly the pause function is being called, but not actually pausing anything. I did some investigation. The function stop() is successful in setting timeoutProm to null, but loopy() calls myCallback before setting the new value of timeoutProm. So while myCallback manages to set timeoutProm to null, timeoutProm still gets set to a valid object before that iteration of loopy() is done.

The fix that I included in this pull request was to add a return statement which checks if loopy() is running when it shouldn't. However, I also tried swapping the order of calling the callback and assigning the new timeoutProm, and that looks like this:

```javascript
(function loopy() {
var item;

stop();

if (!_this.size()) {
cleared = true;
if (angular.isFunction(_this.complete)) {
_this.complete.call(_this);
}
return;
}

item = _this.queue.shift();

timeoutProm = $timeout(loopy,
_this.delay);

_this.callback.call(_this, item);

return;
})();
```

In my (admittedly brief) testing, there is no difference between the two fixes.