Guest User

Untitled

a guest
May 20th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. function zip(...iterables) {
  2. const iterators = iterables.map(i => i[Symbol.iterator]());
  3. let done = false;
  4. return {
  5. [Symbol.iterator]() {
  6. return this;
  7. },
  8. next() {
  9. if (!done) {
  10. const items = iterators.map(i => i.next());
  11. done = items.some(item => item.done);
  12. if (!done) {
  13. return { value: items.map(i => i.value) };
  14. }
  15. // Done for the first time: close all iterators
  16. for (const iterator of iterators) {
  17. if (typeof iterator.return === 'function') {
  18. iterator.return();
  19. }
  20. }
  21. }
  22. // We are done
  23. return { done: true };
  24. }
  25. }
  26. }
Add Comment
Please, Sign In to add comment