wait-for.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* eslint-disable
  2. max-len,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. import App from '../base'
  13. export default App.factory('waitFor', function ($q) {
  14. const waitFor = function (testFunction, timeout, pollInterval) {
  15. if (pollInterval == null) {
  16. pollInterval = 500
  17. }
  18. const iterationLimit = Math.floor(timeout / pollInterval)
  19. let iterations = 0
  20. return $q(function (resolve, reject) {
  21. let tryIteration
  22. return (tryIteration = function () {
  23. if (iterations > iterationLimit) {
  24. return reject(
  25. new Error(
  26. `waiting too long, ${JSON.stringify({ timeout, pollInterval })}`
  27. )
  28. )
  29. }
  30. iterations += 1
  31. const result = testFunction()
  32. if (result != null) {
  33. return resolve(result)
  34. } else {
  35. return setTimeout(tryIteration, pollInterval)
  36. }
  37. })()
  38. })
  39. }
  40. return waitFor
  41. })