asyncIterator.js (1716B)
1 function _asyncIterator(iterable) { 2 var method, 3 async, 4 sync, 5 retry = 2; 6 7 for ("undefined" != typeof Symbol && (async = Symbol.asyncIterator, sync = Symbol.iterator); retry--;) { 8 if (async && null != (method = iterable[async])) return method.call(iterable); 9 if (sync && null != (method = iterable[sync])) return new AsyncFromSyncIterator(method.call(iterable)); 10 async = "@@asyncIterator", sync = "@@iterator"; 11 } 12 13 throw new TypeError("Object is not async iterable"); 14 } 15 16 function AsyncFromSyncIterator(s) { 17 function AsyncFromSyncIteratorContinuation(r) { 18 if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object.")); 19 var done = r.done; 20 return Promise.resolve(r.value).then(function (value) { 21 return { 22 value: value, 23 done: done 24 }; 25 }); 26 } 27 28 return AsyncFromSyncIterator = function AsyncFromSyncIterator(s) { 29 this.s = s, this.n = s.next; 30 }, AsyncFromSyncIterator.prototype = { 31 s: null, 32 n: null, 33 next: function next() { 34 return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments)); 35 }, 36 "return": function _return(value) { 37 var ret = this.s["return"]; 38 return void 0 === ret ? Promise.resolve({ 39 value: value, 40 done: !0 41 }) : AsyncFromSyncIteratorContinuation(ret.apply(this.s, arguments)); 42 }, 43 "throw": function _throw(value) { 44 var thr = this.s["return"]; 45 return void 0 === thr ? Promise.reject(value) : AsyncFromSyncIteratorContinuation(thr.apply(this.s, arguments)); 46 } 47 }, new AsyncFromSyncIterator(s); 48 } 49 50 module.exports = _asyncIterator, module.exports.__esModule = true, module.exports["default"] = module.exports;