summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/behaviors/async-foreach-behavior/async-foreach-behavior.html
diff options
context:
space:
mode:
Diffstat (limited to 'polygerrit-ui/app/behaviors/async-foreach-behavior/async-foreach-behavior.html')
-rw-r--r--polygerrit-ui/app/behaviors/async-foreach-behavior/async-foreach-behavior.html16
1 files changed, 14 insertions, 2 deletions
diff --git a/polygerrit-ui/app/behaviors/async-foreach-behavior/async-foreach-behavior.html b/polygerrit-ui/app/behaviors/async-foreach-behavior/async-foreach-behavior.html
index 01483772bb..36e0201e9e 100644
--- a/polygerrit-ui/app/behaviors/async-foreach-behavior/async-foreach-behavior.html
+++ b/polygerrit-ui/app/behaviors/async-foreach-behavior/async-foreach-behavior.html
@@ -1,4 +1,5 @@
<!--
+@license
Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,12 +26,23 @@ limitations under the License.
/**
* @template T
* @param {!Array<T>} array
- * @param {!Function} fn
+ * @param {!Function} fn An iteratee function to be passed each element of
+ * the array in order. Must return a promise, and the following
+ * iteration will not begin until resolution of the promise returned by
+ * the previous iteration.
+ *
+ * An optional second argument to fn is a callback that will halt the
+ * loop if called.
* @return {!Promise<undefined>}
*/
asyncForeach(array, fn) {
if (!array.length) { return Promise.resolve(); }
- return fn(array[0]).then(() => this.asyncForeach(array.slice(1), fn));
+ let stop = false;
+ const stopCallback = () => { stop = true; };
+ return fn(array[0], stopCallback).then(exit => {
+ if (stop) { return Promise.resolve(); }
+ return this.asyncForeach(array.slice(1), fn);
+ });
},
};
})(window);