asyncIteratorReject.js.flow 855 B

12345678910111213141516171819202122232425262728293031323334
  1. // @flow strict
  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: Error): AsyncIterator<void> {
  9. let isComplete = false;
  10. /* TODO: Flow doesn't support symbols as keys:
  11. https://github.com/facebook/flow/issues/3258 */
  12. return ({
  13. next() {
  14. const result = isComplete
  15. ? Promise.resolve({ value: undefined, done: true })
  16. : Promise.reject(error);
  17. isComplete = true;
  18. return result;
  19. },
  20. return() {
  21. isComplete = true;
  22. return Promise.resolve({ value: undefined, done: true });
  23. },
  24. throw() {
  25. isComplete = true;
  26. return Promise.reject(error);
  27. },
  28. [$$asyncIterator]() {
  29. return this;
  30. },
  31. }: any);
  32. }