Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const flatten = (arr) => [].concat.apply([], arr)
- /**
- * Accepts an iterate collection of promises, resolves them by bundles with accumulate results
- *
- * @param {Array} promises - array of unresolved promises
- * @param {object} settings - object with any settings
- * @param {number} settings.from - number of promise, from which to begin new iteration
- * @param {number} settings.iteration - number of promises for one iteration
- * @returns {Array} - returns promise
- */
- const debouncePromises = async (promises, settings) => {
- const defaults = {
- from: 0,
- iteration: 10
- }
- const options = Object.assign({}, defaults, settings)
- let pack = []
- let results = []
- for (let index = 0; index < promises.length; index += options.iteration) {
- pack = promises.slice(index, index + options.iteration).map(el => el())
- results.push(await Promise.all(pack))
- }
- return flatten(results)
- }
- module.exports = debouncePromises
Add Comment
Please, Sign In to add comment