asyncIteratorReject.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 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; }
  2. import { $$asyncIterator } from 'iterall';
  3. /**
  4. * Given an error, returns an AsyncIterable which will fail with that error.
  5. *
  6. * Similar to Promise.reject(error)
  7. */
  8. export default function asyncIteratorReject(error) {
  9. var isComplete = false;
  10. /* TODO: Flow doesn't support symbols as keys:
  11. https://github.com/facebook/flow/issues/3258 */
  12. return _defineProperty({
  13. next: function next() {
  14. var result = isComplete ? Promise.resolve({
  15. value: undefined,
  16. done: true
  17. }) : Promise.reject(error);
  18. isComplete = true;
  19. return result;
  20. },
  21. return: function _return() {
  22. isComplete = true;
  23. return Promise.resolve({
  24. value: undefined,
  25. done: true
  26. });
  27. },
  28. throw: function _throw() {
  29. isComplete = true;
  30. return Promise.reject(error);
  31. }
  32. }, $$asyncIterator, function () {
  33. return this;
  34. });
  35. }