You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
809 B
JavaScript
28 lines
809 B
JavaScript
"use strict";
|
|
|
|
const Promise = require("bluebird");
|
|
|
|
/* A bit like Promise.each, including being sequential, but at any point the processing callback can invoke a 'dispose' function to throw away the rest of the queue and complete iteration early. Useful when processing of a specific item can invalidate the items that come after it. */
|
|
|
|
module.exports = function runDisposableQueue(items, processingCallback) {
|
|
let itemIndex = 0; // FIXME: overflow
|
|
|
|
function processItem() {
|
|
let disposeFlag = false;
|
|
|
|
return Promise.try(() => {
|
|
let item = items[itemIndex];
|
|
itemIndex += 1;
|
|
|
|
return processingCallback(item, () => {
|
|
disposeFlag = true;
|
|
});
|
|
}).then(() => {
|
|
if (disposeFlag === false && itemIndex < items.length) {
|
|
return processItem();
|
|
}
|
|
});
|
|
}
|
|
|
|
return processItem();
|
|
}; |