# Collection Runner continues to iterate even when postman.setNextRequest(null)

**URL:** <https://community.postman.com/t/collection-runner-continues-to-iterate-even-when-postman-setnextrequest-null/33092>\
**Category:** Help Hub\
**Tags:** collections, setnextrequest, collection-runner\
**Created:** [March 2, 2022, 2:25pm UTC](https://community.postman.com/t/collection-runner-continues-to-iterate-even-when-postman-setnextrequest-null/33092 "2022-03-02T14:25:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![6thSen](https://avatars.discourse-cdn.com/v4/letter/6/5fc32e/32.png) [@6thSen](https://community.postman.com/u/6thSen)\
**Post date:** [March 2, 2022, 2:25pm UTC](https://community.postman.com/t/collection-runner-continues-to-iterate-even-when-postman-setnextrequest-null/33092/1 "2022-03-02T14:25:40Z")

</div>

How do I make the collection runner stop iterating based on the response code I received? I’m already using `postman.setNextRequest(null)` but the iteration still continues.

I’m trying to stop the iteration if the script gets a `pm.response.code` other than `200` or `204`. Other than that, the script just writes the response info to a file (followed this [doc](https://www.postman.com/postman/workspace/postman-answers/collection/3407886-888860dd-9e3c-4537-a78e-1c3f3db7535c?ctx=documentation)).

Here’s my test script:

```auto
let resData = {
    httpResponse: pm.response.code,
    schemeId: pm.iterationData.get("id"),
    name: pm.iterationData.get("name")
};

if(pm.response.code === 200 || pm.response.code === 204) {
    resData.messages = ["Successfully deleted"];
}
else if (pm.response.code === 429) {
    resData.messages = ["Hit API Rate Limit"];
    postman.setNextRequest(null);
}
else {
    resData.messages = pm.response.json().errorMessages;
    postman.setNextRequest(null); // only adding this here for testing, ideally we only want to stop the iteration if we get a 429
};

pm.test("Succesful DELETE request.",function() {
    pm.expect(pm.response.code).to.be.oneOf([201,202,204]);
});

// The opts for the server, also includes the data to be written to file
let opts = {
    requestName: request.name || request.url,
    fileExtension: 'json',
    mode: 'appendFile', // Change this to any function of the fs library of node to use it.
    uniqueIdentifier: false,
    responseData: JSON.stringify(resData) + ","
};

pm.sendRequest({
    url: 'http://localhost:3000/write',
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify(opts)
    }
}, function (err, res) {
    console.log(res);
});

```

This test script is only on the Collection level. There are no scripts on the Folder and Request levels. There are no pre-request scripts on any levels.

The request is only a simple delete request. My test iteration data should give out a 400 error on the first iteration and should not continue further but it still does.

I’ve already tried adding `postman.setNextRequest(null)` to different parts of the code. I found [How to stop Collection Runner on error? - #15 by Merricat](https://community.postman.com/t/how-to-stop-collection-runner-on-error/12647/15) which suggested that I use `postman.setNextRequest()` to clear out the next request after first declaring it as null but that didn’t work either.
