asyncIteratorReject.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = asyncIteratorReject;
  6. var _iterall = require("iterall");
  7. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  8. /**
  9. * Given an error, returns an AsyncIterable which will fail with that error.
  10. *
  11. * Similar to Promise.reject(error)
  12. */
  13. function asyncIteratorReject(error) {
  14. var isComplete = false;
  15. /* TODO: Flow doesn't support symbols as keys:
  16. https://github.com/facebook/flow/issues/3258 */
  17. return _defineProperty({
  18. next: function next() {
  19. var result = isComplete ? Promise.resolve({
  20. value: undefined,
  21. done: true
  22. }) : Promise.reject(error);
  23. isComplete = true;
  24. return result;
  25. },
  26. return: function _return() {
  27. isComplete = true;
  28. return Promise.resolve({
  29. value: undefined,
  30. done: true
  31. });
  32. },
  33. throw: function _throw() {
  34. isComplete = true;
  35. return Promise.reject(error);
  36. }
  37. }, _iterall.$$asyncIterator, function () {
  38. return this;
  39. });
  40. }