Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Postman polling
const addFunction = (func) => {
pm.globals.set('sf-' + func.name, "" + func);
}
const getRetryKey = () => {
return 'retry_[' + request.name + '][' + request.id + ']';
}
const retryClear = () => {
const retryKey = eval(pm.variables.get('sf-getRetryKey'))();
pm.variables.unset(retryKey);
};
const retryIncrement = () => {
const retryKey = eval(pm.variables.get('sf-getRetryKey'))();
if(pm.variables.has(retryKey)){
pm.variables.set(retryKey, pm.variables.get(retryKey) + 1);
} else {
pm.variables.set(retryKey, 0);
}
};
/**
* @param {number} delayMs
* @param {number} maxRetryCount
* @param {function} isRunCallbackFunc: return a boolean, true = run callbackFunc, false = re-call the request
* @param {function} callbackFunc
*/
const retry = (delayMs, maxRetryCount, isRunCallbackFunc, callbackFunc) => {
eval(pm.variables.get('sf-retryIncrement'))();
const retryKey = eval(pm.variables.get('sf-getRetryKey'))();
var retryount = pm.variables.get(retryKey);
const canRetry = retryount <= maxRetryCount;
if(!canRetry){
throw new Error(request.name + ' cannot be retry (retry count = ' + retryount + ')');
}
if(isRunCallbackFunc()) {
eval(pm.variables.get('sf-retryClear'))();
callbackFunc();
setTimeout(function(){}, delayMs);
}
else {
setTimeout(function(){}, delayMs);
postman.setNextRequest(request.name);
}
};
addFunction(retry);
addFunction(retryClear);
addFunction(retryIncrement);
addFunction(getRetryKey);
const isRunCallback = () => responseCode.code === 200;
const callback = () => {
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
}
eval(pm.globals.get('sf-retry'))(1000, 10, isRunCallback, callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.