2 YUI 3.17.2 (build 9c3c78e)
3 Copyright 2014 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('parallel', function (Y, NAME) {
12 * A concurrent parallel processor to help in running several async functions.
18 A concurrent parallel processor to help in running several async functions.
20 var stack = new Y.Parallel();
22 for (var i = 0; i < 15; i++) {
23 Y.io('./api/json/' + i, {
25 success: stack.add(function() {
32 stack.done(function() {
33 Y.log('All IO requests complete!');
37 @param {Object} o A config object
38 @param {Object} [o.context=Y] The execution context of the callback to done
43 Y.Parallel = function(o) {
44 this.config = o || {};
46 this.context = this.config.context || Y;
51 Y.Parallel.prototype = {
53 * An Array of results from all the callbacks in the stack
60 * The total items in the stack
66 * The number of stacked callbacks executed
72 * Add a callback to the stack
74 * @param {Function} fn The function callback we are waiting for
84 self.results[index] = (fn && fn.apply(self.context, arguments)) ||
85 (arguments.length === 1 ? arguments[0] : Y.Array(arguments));
91 * Test to see if all registered items in the stack have completed, if so call the callback to `done`
96 if (self.finished >= self.total && self.callback) {
97 self.callback.call(self.context, self.results, self.data);
101 * The method to call when all the items in the stack are complete.
103 * @param {Function} callback The callback to execute on complete
104 * @param {Mixed} callback.results The results of all the callbacks in the stack
105 * @param {Mixed} [callback.data] The data given to the `done` method
106 * @param {Mixed} data Mixed data to pass to the success callback
108 done: function (callback, data) {
109 this.callback = callback;
116 }, '3.17.2', {"requires": ["yui-base"]});