Guest User

Untitled

a guest
Nov 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const flatten = (arr) => [].concat.apply([], arr)
  4. /**
  5. * Accepts an iterate collection of promises, resolves them by bundles with accumulate results
  6. *
  7. * @param {Array} promises - array of unresolved promises
  8. * @param {object} settings - object with any settings
  9. * @param {number} settings.from - number of promise, from which to begin new iteration
  10. * @param {number} settings.iteration - number of promises for one iteration
  11. * @returns {Array} - returns promise
  12. */
  13. const debouncePromises = async (promises, settings) => {
  14. const defaults = {
  15. from: 0,
  16. iteration: 10
  17. }
  18. const options = Object.assign({}, defaults, settings)
  19.  
  20. let pack = []
  21. let results = []
  22.  
  23. for (let index = 0; index < promises.length; index += options.iteration) {
  24. pack = promises.slice(index, index + options.iteration).map(el => el())
  25. results.push(await Promise.all(pack))
  26. }
  27.  
  28. return flatten(results)
  29. }
  30.  
  31. module.exports = debouncePromises
Add Comment
Please, Sign In to add comment