asyncIterator.js 1.7 KB

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