wrapAsyncGenerator.js 1.9 KB

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