UNPKG

@reactivex/ix-esnext-esm

Version:

The Interactive Extensions for JavaScript

55 lines (53 loc) 1.75 kB
import { AsyncIterableX } from './asynciterablex'; class RaceAsyncIterable extends AsyncIterableX { constructor(left, right) { super(); this._left = left; this._right = right; } async *[Symbol.asyncIterator]() { const leftIt = this._left[Symbol.asyncIterator](); const rightIt = this._right[Symbol.asyncIterator](); let otherIterator; let resultIterator; const { value, done } = await Promise.race([ leftIt.next().then(x => { if (!resultIterator) { resultIterator = leftIt; otherIterator = rightIt; } return x; }), rightIt.next().then(x => { if (!resultIterator) { resultIterator = rightIt; otherIterator = leftIt; } return x; }) ]); if (!done) { yield value; } otherIterator = otherIterator; resultIterator = resultIterator; // Cancel/finish other iterator if (otherIterator.return) { await otherIterator.return(); } let next; while (!(next = await resultIterator.next()).done) { yield next.value; } } } /** * Propagates the async sequence that reacts first. * @param {AsyncIterable<T>} left First async sequence. * @param {AsyncIterable<T>} right Second async sequence. * @return {AsyncIterable<T>} An async sequence that surfaces either of the given sequences, whichever reacted first. */ export function race(left, right) { return new RaceAsyncIterable(left, right); } //# sourceMappingURL=race.mjs.map