Repeat request until response will return proper value

When testing my API the logic is that a task is being locked. During this time backend is doing some calculations (I don’t have control over it). When finished - an endpoint will return: isLocked: false, otherwise is returning all the time isLocked: true. I need to stop collection requests execution until task is no longer locked and I am using it few times within same collection.
I have created a custom function to handle such scenario which works in most of cases, but in some of them is not working - meaning postman / newman will wait until task is no longer locked, but then will skip next (subsequent) request which obviously will fail the tests.

const responseJson = pm.response.json();

var taskId = pm.environment.get("taskId")

function checkIfTaskIsUnlocked(responseJson) {
    if (responseJson.items && responseJson.items[taskId]) {
        if (responseJson.items[taskId].isLocked === false) {
              return true;
        } else {
            console.log('Task is still locked');
            return false;
        }
    } else {
        console.error('Task ID not found in the response JSON');
        return false;
    }
}



if (!checkIfTaskIsUnlocked(responseJson)) {
    console.log('Retrying the request in 10 seconds...');

    setTimeout(function() {
        pm.execution.setNextRequest(pm.info.requestName);
    }, 10000);

Where is taskId being set?

There is nothing in that code that will skip the next request.

Can you clarify what you mean by “but in some of them is not working”.

What is the console log showing for those instances?

What tests is it failing. If it skips the next request, it will also skip the tests associated to that request.

Perhaps some screenshots would help show the issue more clearly.

Where is taskId being set?

Just added the missing line - taskId is taken from environment variables.

Can you clarify what you mean by “but in some of them is not working”.
What is the console log showing for those instances?

Let say I am using exact same request (function) 6 times within a collection. For the first 5-times of usage it works properly - meaning it waits for task to be unlocked and proceed with further subsequent request.
The 6th time of usage - it waits for task to be unlocked, but then it is skipping next subsequent (very relevant) request within collection.

I am using exact same script, there is nothing else in post-script and nothing in pre-script.

Here is screenshot with console which shows that it is skipping two subsequent requests:

Your screenshot seems to show that it looped on the request twice, but then went straight to the GET request and bypassed the two POST requests inbetween.

setNextRequest just sets the next request that will run after the current request and all of the scripts have completed (pre and post response scripts).

I can’t see any reason that would happen and skip requests without having some other logic or script.

If you have setNextRequest() in multiple places then the command run last will win out.

I would also check the folder and collection to ensure you don’t have any scripts at those levels in the mix.

If the condition is not triggered as per your code example suggests, the setNextRequest() function should not be hit and its should just continue running the requests in your collection in order.

I can only suggest implementing as many console logs as you can so you see what is happening. Add one just before it returns true, so you can see that this part is actually being triggered so it shows in the console logs like your false triggers.

Perhaps create a folder, with a more simplified structure, and test the setNextRequest logic with that. It might be easier to troubleshoot. Perhaps create a small flow with static data to ensure that those requests aren’t being skipped for another reason.

I’m not really sure that you need to wrap all of this up in a function.

You could have your initial IF statement checking to see if you have a valid response with a task ID, and then have an IF statement that checks for isLocked === true and then do the timeout and setNextRequest. Don’t really need the ELSE’s. I would get it workout outside of a function and then turn it into a function after you have it working (although I don’t really think this function is causing the problem).

Or you check for valid response and that the isLocked in the same statement.

Also, I’m not sure that code will work properly if the response is not correct. If items does not exist then it will crash and cause a fatal error instead of returning undefined. The recommended way is to check if something exists without it causing a fatal error is using typeof.

if (!typeof responseJson.items[taskId] === 'undefined' & responseJson.items[taskId].isLocked === true) {}