UNPKG

iter-over

Version:

Sugary iteration utilities and interfaces.

3 lines (2 loc) 1.69 kB
"use strict";var __awaiter=this&&this.__awaiter||function(t,e,r,s){return new(r||(r=Promise))((function(i,o){function n(t){try{c(s.next(t))}catch(t){o(t)}}function a(t){try{c(s.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(n,a)}c((s=s.apply(t,e||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.AsyncIterator=void 0;const abstract_iterator_1=require("../core/abstract-iterator");class AsyncIterator extends abstract_iterator_1.AbstractIterator{constructor(t){super(),this.promiseIndex=0,this.promiseStatusIndicators=[],this.settledPromises=[];for(let e=0;e<t.length;e++){let r,s,i=new Promise(((t,e)=>{r=t,s=e}));this.promiseStatusIndicators[e]={promise:i,resolve:r,reject:s},t[e].then((r=>{let s=this.settledPromises.push(t[e]);this.promiseStatusIndicators[s-1].resolve(r)}),(r=>{let s=this.settledPromises.push(t[e]);this.promiseStatusIndicators[s-1].reject(r)}))}}hasNext(){return this.promiseIndex<this.promiseStatusIndicators.length}next(){return __awaiter(this,void 0,void 0,(function*(){if(this.hasNext())return yield this.promiseStatusIndicators[this.promiseIndex].promise,this.settledPromises[this.promiseIndex++]}))}remove(){}reset(){this.promiseIndex=0}[Symbol.asyncIterator](){return new class{constructor(t){this.iterator=t}[Symbol.asyncIterator](){return this}next(){return __awaiter(this,void 0,void 0,(function*(){return this.iterator.hasNext()?{done:!1,value:yield this.iterator.next()}:{done:!0,value:void 0}}))}}(this)}getAsyncIterableIterator(){return this[Symbol.asyncIterator]()}}exports.AsyncIterator=AsyncIterator; //# sourceMappingURL=async-iterator.js.map