OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to sequentially chain two sync iterators in JavaScript?

  • Thread starter Thread starter Alexander Lin
  • Start date Start date
A

Alexander Lin

Guest
Let's say we have two async iterators,

Code:
const asyncIterable1 = {
  [Symbol.asyncIterator]() {
    return {
      i: 0,
      next() {
      if (this.i < 3) {
        return Promise.resolve({ value: this.i++, done: false });
      }

      return Promise.resolve({ done: true });
    }
  };
 }
};

const asyncIterable2 = {
  [Symbol.asyncIterator]() {
    return {
      i: 3,
      next() {
      if (this.i < 5) {
        return Promise.resolve({ value: this.i++, done: false });
      }

      return Promise.resolve({ done: true });
    }
  };
 }
};

Now, is there a way to combine these two iterators into one iterator that would return a sequence of 0,1,2 and then 3,4?

<p>Let's say we have two async iterators,</p>
<pre><code>const asyncIterable1 = {
[Symbol.asyncIterator]() {
return {
i: 0,
next() {
if (this.i < 3) {
return Promise.resolve({ value: this.i++, done: false });
}

return Promise.resolve({ done: true });
}
};
}
};

const asyncIterable2 = {
[Symbol.asyncIterator]() {
return {
i: 3,
next() {
if (this.i < 5) {
return Promise.resolve({ value: this.i++, done: false });
}

return Promise.resolve({ done: true });
}
};
}
};
</code></pre>
<p>Now, is there a way to combine these two iterators into one iterator that would return a sequence of 0,1,2 and then 3,4?</p>
 
Top