Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. 'use strict';
  2.  
  3. import { AsyncIterableX } from '../asynciterable';
  4.  
  5. class TakeUntilAsyncIterable<TSource> extends AsyncIterableX<TSource> {
  6. private _source: AsyncIterable<TSource>;
  7. private _other: Promise<any>;
  8.  
  9. constructor(source: AsyncIterable<TSource>, other: Promise<any>) {
  10. super();
  11. this._source = source;
  12. this._other = other;
  13. }
  14.  
  15. async *[Symbol.asyncIterator]() {
  16. const it = this._source[Symbol.asyncIterator]();
  17. let otherDone = false, next;
  18. this._other.then(() => otherDone = true).catch(() => otherDone = true);
  19. while (!otherDone && !(next = await it.next()).done) {
  20. yield next.value;
  21. }
  22. }
  23. }
  24.  
  25. export function takeUntil<TSource>(
  26. source: AsyncIterable<TSource>,
  27. other: Promise<any>): AsyncIterable<TSource> {
  28. return new TakeUntilAsyncIterable<TSource>(source, other);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement