asyncGenerator.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. exports.__esModule = true;
  3. var _symbol = require("../core-js/symbol");
  4. var _symbol2 = _interopRequireDefault(_symbol);
  5. var _promise = require("../core-js/promise");
  6. var _promise2 = _interopRequireDefault(_promise);
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. exports.default = function () {
  9. function AwaitValue(value) {
  10. this.value = value;
  11. }
  12. function AsyncGenerator(gen) {
  13. var front, back;
  14. function send(key, arg) {
  15. return new _promise2.default(function (resolve, reject) {
  16. var request = {
  17. key: key,
  18. arg: arg,
  19. resolve: resolve,
  20. reject: reject,
  21. next: null
  22. };
  23. if (back) {
  24. back = back.next = request;
  25. } else {
  26. front = back = request;
  27. resume(key, arg);
  28. }
  29. });
  30. }
  31. function resume(key, arg) {
  32. try {
  33. var result = gen[key](arg);
  34. var value = result.value;
  35. if (value instanceof AwaitValue) {
  36. _promise2.default.resolve(value.value).then(function (arg) {
  37. resume("next", arg);
  38. }, function (arg) {
  39. resume("throw", arg);
  40. });
  41. } else {
  42. settle(result.done ? "return" : "normal", result.value);
  43. }
  44. } catch (err) {
  45. settle("throw", err);
  46. }
  47. }
  48. function settle(type, value) {
  49. switch (type) {
  50. case "return":
  51. front.resolve({
  52. value: value,
  53. done: true
  54. });
  55. break;
  56. case "throw":
  57. front.reject(value);
  58. break;
  59. default:
  60. front.resolve({
  61. value: value,
  62. done: false
  63. });
  64. break;
  65. }
  66. front = front.next;
  67. if (front) {
  68. resume(front.key, front.arg);
  69. } else {
  70. back = null;
  71. }
  72. }
  73. this._invoke = send;
  74. if (typeof gen.return !== "function") {
  75. this.return = undefined;
  76. }
  77. }
  78. if (typeof _symbol2.default === "function" && _symbol2.default.asyncIterator) {
  79. AsyncGenerator.prototype[_symbol2.default.asyncIterator] = function () {
  80. return this;
  81. };
  82. }
  83. AsyncGenerator.prototype.next = function (arg) {
  84. return this._invoke("next", arg);
  85. };
  86. AsyncGenerator.prototype.throw = function (arg) {
  87. return this._invoke("throw", arg);
  88. };
  89. AsyncGenerator.prototype.return = function (arg) {
  90. return this._invoke("return", arg);
  91. };
  92. return {
  93. wrap: function wrap(fn) {
  94. return function () {
  95. return new AsyncGenerator(fn.apply(this, arguments));
  96. };
  97. },
  98. await: function _await(value) {
  99. return new AwaitValue(value);
  100. }
  101. };
  102. }();