| 1 |
|
| 2 |
|
| 3 |
export default function _asyncIterator(iterable) { |
| 4 |
var method, |
| 5 |
async, |
| 6 |
sync, |
| 7 |
retry = 2; |
| 8 |
|
| 9 |
if (typeof Symbol !== "undefined") { |
| 10 |
async = Symbol.asyncIterator; |
| 11 |
sync = Symbol.iterator; |
| 12 |
} |
| 13 |
|
| 14 |
while (retry--) { |
| 15 |
if (async && (method = iterable[async]) != null) { |
| 16 |
return method.call(iterable); |
| 17 |
} |
| 18 |
if (sync && (method = iterable[sync]) != null) { |
| 19 |
return new AsyncFromSyncIterator(method.call(iterable)); |
| 20 |
} |
| 21 |
|
| 22 |
async = "@@asyncIterator"; |
| 23 |
sync = "@@iterator"; |
| 24 |
} |
| 25 |
|
| 26 |
throw new TypeError("Object is not async iterable"); |
| 27 |
} |
| 28 |
|
| 29 |
function AsyncFromSyncIterator(s) { |
| 30 |
AsyncFromSyncIterator = function (s) { |
| 31 |
this.s = s; |
| 32 |
this.n = s.next; |
| 33 |
}; |
| 34 |
AsyncFromSyncIterator.prototype = { |
| 35 |
s: null, |
| 36 |
n: null, |
| 37 |
next: function () { |
| 38 |
return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments)); |
| 39 |
}, |
| 40 |
return: function (value) { |
| 41 |
var ret = this.s.return; |
| 42 |
if (ret === undefined) { |
| 43 |
return Promise.resolve({ value: value, done: true }); |
| 44 |
} |
| 45 |
return AsyncFromSyncIteratorContinuation(ret.apply(this.s, arguments)); |
| 46 |
}, |
| 47 |
throw: function (value) { |
| 48 |
var thr = this.s.return; |
| 49 |
if (thr === undefined) return Promise.reject(value); |
| 50 |
return AsyncFromSyncIteratorContinuation(thr.apply(this.s, arguments)); |
| 51 |
}, |
| 52 |
}; |
| 53 |
|
| 54 |
function AsyncFromSyncIteratorContinuation(r) { |
| 55 |
|
| 56 |
if (Object(r) !== r) { |
| 57 |
return Promise.reject(new TypeError(r + " is not an object.")); |
| 58 |
} |
| 59 |
|
| 60 |
var done = r.done; |
| 61 |
return Promise.resolve(r.value).then(function (value) { |
| 62 |
return { value: value, done: done }; |
| 63 |
}); |
| 64 |
} |
| 65 |
|
| 66 |
return new AsyncFromSyncIterator(s); |
| 67 |
} |