Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. MAX_POLL_COUNT = 5;
  2.  
  3. getCount = function() {
  4. var count = postman.getEnvironmentVariable('__poll_count');
  5. return (count === undefined) ? 0 : JSON.parse(count);
  6. }
  7.  
  8. setCount = value => postman.setEnvironmentVariable('__poll_count', JSON.stringify(value));
  9.  
  10. increment = () => setCount(getCount() + 1);
  11.  
  12. reset = () => postman.clearEnvironmentVariable('__poll_count');
  13.  
  14. abort = function() {
  15. reset();
  16. postman.setNextRequest(null);
  17. }
  18.  
  19. maxCountReached = () => getCount() >= MAX_POLL_COUNT;
  20.  
  21. poll = function(name, predicate) {
  22. let keepPolling = predicate(response);
  23. if (maxCountReached()) {
  24. abort();
  25. } else if (keepPolling) {
  26. increment();
  27. postman.setNextRequest(name);
  28. new Promise(resolve => setTimeout(resolve, 1000));
  29. } else {
  30. reset();
  31. return true;
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement