Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function zip(...iterables) {
- const iterators = iterables.map(i => i[Symbol.iterator]());
- let done = false;
- return {
- [Symbol.iterator]() {
- return this;
- },
- next() {
- if (!done) {
- const items = iterators.map(i => i.next());
- done = items.some(item => item.done);
- if (!done) {
- return { value: items.map(i => i.value) };
- }
- // Done for the first time: close all iterators
- for (const iterator of iterators) {
- if (typeof iterator.return === 'function') {
- iterator.return();
- }
- }
- }
- // We are done
- return { done: true };
- }
- }
- }
Add Comment
Please, Sign In to add comment