AsyncGenerator.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import OverloadYield from "./OverloadYield.js";
  2. export default function AsyncGenerator(e) {
  3. var r, t;
  4. function resume(r, t) {
  5. try {
  6. var n = e[r](t),
  7. o = n.value,
  8. u = o instanceof OverloadYield;
  9. Promise.resolve(u ? o.v : o).then(function (t) {
  10. if (u) {
  11. var i = "return" === r ? "return" : "next";
  12. if (!o.k || t.done) return resume(i, t);
  13. t = e[i](t).value;
  14. }
  15. settle(n.done ? "return" : "normal", t);
  16. }, function (e) {
  17. resume("throw", e);
  18. });
  19. } catch (e) {
  20. settle("throw", e);
  21. }
  22. }
  23. function settle(e, n) {
  24. switch (e) {
  25. case "return":
  26. r.resolve({
  27. value: n,
  28. done: !0
  29. });
  30. break;
  31. case "throw":
  32. r.reject(n);
  33. break;
  34. default:
  35. r.resolve({
  36. value: n,
  37. done: !1
  38. });
  39. }
  40. (r = r.next) ? resume(r.key, r.arg) : t = null;
  41. }
  42. this._invoke = function (e, n) {
  43. return new Promise(function (o, u) {
  44. var i = {
  45. key: e,
  46. arg: n,
  47. resolve: o,
  48. reject: u,
  49. next: null
  50. };
  51. t ? t = t.next = i : (r = t = i, resume(e, n));
  52. });
  53. }, "function" != typeof e["return"] && (this["return"] = void 0);
  54. }
  55. AsyncGenerator.prototype["function" == typeof Symbol && Symbol.asyncIterator || "@@asyncIterator"] = function () {
  56. return this;
  57. }, AsyncGenerator.prototype.next = function (e) {
  58. return this._invoke("next", e);
  59. }, AsyncGenerator.prototype["throw"] = function (e) {
  60. return this._invoke("throw", e);
  61. }, AsyncGenerator.prototype["return"] = function (e) {
  62. return this._invoke("return", e);
  63. };