# Loop over setNextRequest() function

**URL:** <https://community.postman.com/t/loop-over-setnextrequest-function/22175>\
**Category:** Help Hub\
**Tags:** tests, collections\
**Created:** [April 1, 2021, 12:49pm UTC](https://community.postman.com/t/loop-over-setnextrequest-function/22175 "2021-04-01T12:49:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuadhl-ee](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@fuadhl-ee](https://community.postman.com/u/fuadhl-ee)\
**Post date:** [April 1, 2021, 12:49pm UTC](https://community.postman.com/t/loop-over-setnextrequest-function/22175/1 "2021-04-01T12:49:52Z")

</div>

Hi, I just create one logic. I receive array from API and make a loop to test them. Exactly this kind of loop.  
list.map(item =\> {  
if (item.type === 1) {  
pm.test(`#######`, () =\> {  
postman.setNextRequest("#####")  
});  
} else if (item.type === 2) {  
pm.test(`###`, () =\> {  
postman.setNextRequest("###")  
})  
} else {  
pm.test(`######`, () =\> {  
postman.setNextRequest(’###’)  
})  
}  
})

I expect it will setNextRequest when postman see this condition. As an example if i have same type 4 out of items it means it should send 4times but actually setNextRequest() didn’t worked.

Do we have such kind of functionality

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [April 2, 2021, 9:12pm UTC](https://community.postman.com/t/loop-over-setnextrequest-function/22175/2 "2021-04-02T21:12:27Z")

</div>

Setnextrequest doesn’t work as a queued process . It works as the last command wins .

So even if you call setnextrequest 100 times , the setnextrequest will be executed only once that’s for the last one (100th) .

---

<div class="post-metadata">

**Author:** ![fuadhl-ee](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@fuadhl-ee](https://community.postman.com/u/fuadhl-ee)\
**Post date:** [April 28, 2021, 7:23am UTC](https://community.postman.com/t/loop-over-setnextrequest-function/22175/3 "2021-04-28T07:23:47Z")

</div>

@praveendvd Thanks, i know this rule but want to hear some solution that i can solve the issue

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [April 28, 2021, 9:25am UTC](https://community.postman.com/t/loop-over-setnextrequest-function/22175/4 "2021-04-28T09:25:15Z")

</div>

Store the response array to variable

add this to your test script

```
pm.variables.get("array") ? null : pm.variables.set("array", item)

let counter = pm.variables.get("array")

let type = counter.pop()

switch (type) {

    case 1:

        postman.setNextRequest("1")

        break

    case 2:

        postman.setNextRequest("2")

        break

    case 3:

        postman.setNextRequest("3")

        break

}

```

pm.variables.get(“array”) ? null : pm.variables.set(“array”, item)

let counter = pm.variables.get(“array”)

let type = counter.pop()

switch (type) {

```
case 1:

    postman.setNextRequest("1")

    break

case 2:

    postman.setNextRequest("2")

    break

case 3:

    postman.setNextRequest("3")

    break

```

}

counter.length ? postman.setNextRequest(pm.info.name) : postman.setNextRequest(null)
