UNPKG

bigpipe.js

Version:

The client-side library which is used in BigPipe to orchestrate the pagelets.

52 lines (43 loc) 1.29 kB
'use strict'; var collection = require('./collection'); // // Pointless function that will replace callbacks once they are executed to // prevent double execution from ever happening. // function noop() { /* you waste your time by reading this, see, I told you.. */ } /** * Asynchronously iterate over the given data. * * @param {Mixed} data The data we need to iterate over * @param {Function} iterator Function that's called for each item. * @param {Function} fn The completion callback * @param {Object} options Async options. * @api public */ exports.each = function each(data, iterator, fn, options) { options = options || {}; var size = collection.size(data) , completed = 0 , timeout; if (!size) return fn(); collection.each(data, function iterating(item) { iterator.call(options.context || iterator, item, function done(err) { if (err) { fn(err); return fn = noop; } if (++completed === size) { fn(); if (timeout) clearTimeout(timeout); return fn = noop; } }); }); // // Optional timeout for when the operation takes to long. // if (options.timeout) timeout = setTimeout(function kill() { fn(new Error('Operation timed out')); fn = noop; }, options.timeout); };