# Postman loop over request with while loop

**URL:** <https://community.postman.com/t/postman-loop-over-request-with-while-loop/32267>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [February 6, 2022, 4:29am UTC](https://community.postman.com/t/postman-loop-over-request-with-while-loop/32267 "2022-02-06T04:29:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![security-cosmologis4](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@security-cosmologis4](https://community.postman.com/u/security-cosmologis4)\
**Post date:** [February 6, 2022, 4:29am UTC](https://community.postman.com/t/postman-loop-over-request-with-while-loop/32267/1 "2022-02-06T04:29:36Z")

</div>

There is a topic here explaining looping over a request over and over with a while loop.

This seems wrong/incorrect to me. Please let me know. In this case it just looks like they are running setNextRequest(); ten times and then that request will keep looping forever.

> **[Building request workflows](https://learning.postman.com/docs/running-collections/building-workflows/)**

 ![Screen Shot 2022-02-05 at 8.25.15 PM](https://us1.discourse-cdn.com/getpostman/original/2X/5/531b8765c80f1dc7cf018e6b611756ff321d795f.jpeg)

---

<div class="post-metadata">

**Author:** ![dids-reyes](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@dids-reyes](https://community.postman.com/u/dids-reyes)\
**Post date:** [January 13, 2024, 2:18pm UTC](https://community.postman.com/t/postman-loop-over-request-with-while-loop/32267/2 "2024-01-13T14:18:05Z")

</div>

tried this in for loop and it did not exit the loop

```auto
for(i = 0; i < 9; i++){
    postman.setNextRequest("request_name");
    console.log("Count" + i);
}

```

how do you loop in a request correctly?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 15, 2024, 10:51am UTC](https://community.postman.com/t/postman-loop-over-request-with-while-loop/32267/3 "2024-01-15T10:51:24Z")

</div>

SetNextRequest just sets the next request that will run after all of the code in the current tab has completed.

In your example code, it will just loop through the nine iterations quickly and only set the one request.

Which if its the same request with this code in, will just loop and loop for ever.

You need to set a collection variable as a counter, and then use that variable in conjunction with an IF statement to control the loop.

No need for “for” or “while” loops. Just an IF statement checking the counter value. You would just need to increase the counter and have the setNextRequest in the IF statement code block.
