AsyncGenerator.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var _Promise = require("@babel/runtime-corejs3/core-js/promise");
  2. var _Symbol = require("@babel/runtime-corejs3/core-js/symbol");
  3. var _Symbol$asyncIterator = require("@babel/runtime-corejs3/core-js/symbol/async-iterator");
  4. var AwaitValue = require("./AwaitValue.js");
  5. function AsyncGenerator(gen) {
  6. var front, back;
  7. function send(key, arg) {
  8. return new _Promise(function (resolve, reject) {
  9. var request = {
  10. key: key,
  11. arg: arg,
  12. resolve: resolve,
  13. reject: reject,
  14. next: null
  15. };
  16. if (back) {
  17. back = back.next = request;
  18. } else {
  19. front = back = request;
  20. resume(key, arg);
  21. }
  22. });
  23. }
  24. function resume(key, arg) {
  25. try {
  26. var result = gen[key](arg);
  27. var value = result.value;
  28. var wrappedAwait = value instanceof AwaitValue;
  29. _Promise.resolve(wrappedAwait ? value.wrapped : value).then(function (arg) {
  30. if (wrappedAwait) {
  31. resume(key === "return" ? "return" : "next", arg);
  32. return;
  33. }
  34. settle(result.done ? "return" : "normal", arg);
  35. }, function (err) {
  36. resume("throw", err);
  37. });
  38. } catch (err) {
  39. settle("throw", err);
  40. }
  41. }
  42. function settle(type, value) {
  43. switch (type) {
  44. case "return":
  45. front.resolve({
  46. value: value,
  47. done: true
  48. });
  49. break;
  50. case "throw":
  51. front.reject(value);
  52. break;
  53. default:
  54. front.resolve({
  55. value: value,
  56. done: false
  57. });
  58. break;
  59. }
  60. front = front.next;
  61. if (front) {
  62. resume(front.key, front.arg);
  63. } else {
  64. back = null;
  65. }
  66. }
  67. this._invoke = send;
  68. if (typeof gen["return"] !== "function") {
  69. this["return"] = undefined;
  70. }
  71. }
  72. AsyncGenerator.prototype[typeof _Symbol === "function" && _Symbol$asyncIterator || "@@asyncIterator"] = function () {
  73. return this;
  74. };
  75. AsyncGenerator.prototype.next = function (arg) {
  76. return this._invoke("next", arg);
  77. };
  78. AsyncGenerator.prototype["throw"] = function (arg) {
  79. return this._invoke("throw", arg);
  80. };
  81. AsyncGenerator.prototype["return"] = function (arg) {
  82. return this._invoke("return", arg);
  83. };
  84. module.exports = AsyncGenerator;
  85. module.exports["default"] = module.exports, module.exports.__esModule = true;