enqueueWithResults.js 944 B

12345678910111213141516171819202122232425262728
  1. /**
  2. Returns a function which executes promises one after another. The resulting function
  3. returns a promise, which gets filled with an array of the results of the single promises.
  4. @param promiseGenerators an array of functions which return a promise
  5. @return function which executes the promises
  6. */
  7. const enqueueWithResults = (function () {
  8. const fnQueue = (results, promiseGenerators) => promiseGenerators.reduce((f, promiseGenerator) => {
  9. return () => {
  10. return f().then(result => {
  11. results.push(result);
  12. return promiseGenerator();
  13. });
  14. };
  15. });
  16. return function (promiseGenerators) {
  17. return () => {
  18. const results = [];
  19. return fnQueue(results, promiseGenerators)().then(result => {
  20. results.push(result);
  21. return results;
  22. });
  23. };
  24. };
  25. }());
  26. export default enqueueWithResults;