_async_iterator.js 1.8 KB

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