Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MAX_POLL_COUNT = 5;
- getCount = function() {
- var count = postman.getEnvironmentVariable('__poll_count');
- return (count === undefined) ? 0 : JSON.parse(count);
- }
- setCount = value => postman.setEnvironmentVariable('__poll_count', JSON.stringify(value));
- increment = () => setCount(getCount() + 1);
- reset = () => postman.clearEnvironmentVariable('__poll_count');
- abort = function() {
- reset();
- postman.setNextRequest(null);
- }
- maxCountReached = () => getCount() >= MAX_POLL_COUNT;
- poll = function(name, predicate) {
- let keepPolling = predicate(response);
- if (maxCountReached()) {
- abort();
- } else if (keepPolling) {
- increment();
- postman.setNextRequest(name);
- new Promise(resolve => setTimeout(resolve, 1000));
- } else {
- reset();
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement