/*global Promise:true*/
var Promise = require('bluebird');

var workQueue = {
    queue: [],
    drain: function () {
        this.queue.forEach(function (fn) {
            fn();
        });
        this.queue = [];
    }
};

var scheduler = Promise.setScheduler(function (fn) {
    workQueue.queue.push(fn);
    scheduler(function () {
        workQueue.drain();
    });
});


var _notifyUnhandledRejection = Promise.prototype._notifyUnhandledRejection;
Promise.prototype._notifyUnhandledRejection = function () {
    var that = this;
    var args = arguments;
    scheduler(function () {
        _notifyUnhandledRejection.apply(that, args);
    });
};

module.exports = workQueue;

/*global Promise:true*/
var Promise = require('bluebird');

var workQueue = {
    queue: [],
    drain: function () {
        this.queue.forEach(function (fn) {
            fn();
        });
        this.queue = [];
    }
};

var scheduler = Promise.setScheduler(function (fn) {
    workQueue.queue.push(fn);
    scheduler(function () {
        workQueue.drain();
    });
});


var _notifyUnhandledRejection = Promise.prototype._notifyUnhandledRejection;
Promise.prototype._notifyUnhandledRejection = function () {
    var that = this;
    var args = arguments;
    scheduler(function () {
        if (that._isRejectionUnhandled()) {
            throw that.reason();
        }
    });
};

module.exports = workQueue;

Make sure that unhandled rejections are thrown so that eg. mocha will pick them up and consider the test suite to have failed.
Make sure that unhandled rejections are thrown so that eg. mocha will pick them up and consider the test suite to have failed.
