asyncGeneratorDelegate.js 922 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. export default (function (inner, awaitWrap) {
  2. var iter = {},
  3. waiting = false;
  4. function pump(key, value) {
  5. waiting = true;
  6. value = new Promise(function (resolve) {
  7. resolve(inner[key](value));
  8. });
  9. return {
  10. done: false,
  11. value: awaitWrap(value)
  12. };
  13. }
  14. ;
  15. if (typeof Symbol === "function" && Symbol.iterator) {
  16. iter[Symbol.iterator] = function () {
  17. return this;
  18. };
  19. }
  20. iter.next = function (value) {
  21. if (waiting) {
  22. waiting = false;
  23. return value;
  24. }
  25. return pump("next", value);
  26. };
  27. if (typeof inner.throw === "function") {
  28. iter.throw = function (value) {
  29. if (waiting) {
  30. waiting = false;
  31. throw value;
  32. }
  33. return pump("throw", value);
  34. };
  35. }
  36. if (typeof inner.return === "function") {
  37. iter.return = function (value) {
  38. return pump("return", value);
  39. };
  40. }
  41. return iter;
  42. });