# Waiting for elements in the response goes to infinite loop

**URL:** <https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583>\
**Category:** Help Hub\
**Created:** [November 11, 2020, 12:54am UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583 "2020-11-11T00:54:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![oasis](https://avatars.discourse-cdn.com/v4/letter/o/a698b9/32.png) [@oasis](https://community.postman.com/u/oasis)\
**Post date:** [November 11, 2020, 12:54am UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/1 "2020-11-11T00:54:00Z")

</div>

I was wondering if anyone could help me out here:) I have a POST request to make a booking and then a Get request to fetch ‘below booking status’ -  
BOOK\_RECEIVED, BOOKING and BOOKED  
response json:-

```auto
{
    "statusHistory": [
        {
            "message": "booking received",
            "datetime": "2020-11-11T00:39:15.043Z",
            "statusCode": "BOOK_RECEIVED"
        },
        {
            "message": "booking",
            "datetime": "2020-11-11T00:39:15.149Z",
            "statusCode": "BOOKING"
        },
        {
            "message": "booked",
            "datetime": "2020-11-11T00:39:20.701Z",
            "statusCode": "BOOKED"
        }
    ]
}

```

It takes a few seconds to move from BOOK\_RECEIVED to BOOKING to BOOKED.  
So, I have added the below code to the ‘Tests’ in the Get request to poll:-

```
var getStatus = function(){
     bodyData=JSON.parse(responseBody);
      var statusArray= bodyData.statusHistory.map(
            function(filter) {
                return filter.statusCode; 
            });
            return statusArray;
  }

while(getStatus().length<3){  
      setTimeout(()=>{},1000);
      postman.setNextRequest(pm.info.requestId);
}

```

The issue I face is that it goes into an infinite loop and I can’t figure out why. Could anyone suggest a better way of handling it or how to fix this one?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 11, 2020, 5:27am UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/2 "2020-11-11T05:27:09Z")

</div>

What is `getStatus()` returning? Have you logged that out to know what it returns an array?

---

<div class="post-metadata">

**Author:** ![athimniiheb](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@athimniiheb](https://community.postman.com/u/athimniiheb)\
**Post date:** [November 11, 2020, 6:53am UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/3 "2020-11-11T06:53:15Z")

</div>

Hello  
I think the main problem is the length of the array  
The length fonction give you the max number that the array can hold  
To investigate the problem  
Remove the while and check that you get the right length of the statuscode you have  
After you check the length you get check the compare you have with 3  
Hope that help you out

---

<div class="post-metadata">

**Author:** ![oasis](https://avatars.discourse-cdn.com/v4/letter/o/a698b9/32.png) [@oasis](https://community.postman.com/u/oasis)\
**Post date:** [November 11, 2020, 9:26am UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/4 "2020-11-11T09:26:08Z")

</div>

@danny-dainton, getStatus() returns those three statuses. I have checked (outside the loop) and it does return BOOK\_RECEIVED, BOOKING and BOOKED. The issue is all those statuses aren’t available straight away. So, I use the loop until the array length is 3. This is where it goes to infinite loop.

---

<div class="post-metadata">

**Author:** ![oasis](https://avatars.discourse-cdn.com/v4/letter/o/a698b9/32.png) [@oasis](https://community.postman.com/u/oasis)\
**Post date:** [November 11, 2020, 9:28am UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/5 "2020-11-11T09:28:32Z")

</div>

I have checked and it does return the length of the array available. But the issue is I don’t get all statuses straightaway. So, need to poll for few seconds to get the length==3. This is where it goes to infinite loop

---

<div class="post-metadata">

**Author:** ![oasis](https://avatars.discourse-cdn.com/v4/letter/o/a698b9/32.png) [@oasis](https://community.postman.com/u/oasis)\
**Post date:** [November 11, 2020, 9:44am UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/6 "2020-11-11T09:44:39Z")

</div>

You can see in the screenshot that it has returned two statuses straightaway. Then I use the loop until I have 3. At least that’s what I am trying to achieve.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/4/94b59179d7647427884ca4fcaa688429896a5383.png)

---

<div class="post-metadata">

**Author:** ![athimniiheb](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@athimniiheb](https://community.postman.com/u/athimniiheb)\
**Post date:** [November 11, 2020, 12:40pm UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/7 "2020-11-11T12:40:41Z")

</div>

> [@oasis](#):
>
> getStatus()

Hello again  
Check the return value of “ getStatus().length “ in the log output ??

---

<div class="post-metadata">

**Author:** ![oasis](https://avatars.discourse-cdn.com/v4/letter/o/a698b9/32.png) [@oasis](https://community.postman.com/u/oasis)\
**Post date:** [November 11, 2020, 12:59pm UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/8 "2020-11-11T12:59:21Z")

</div>

It does output the correct number of elements in the array.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 11, 2020, 2:32pm UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/9 "2020-11-11T14:32:28Z")

</div>

I was just messing about and haven’t tried this out yet but would something basic like this work:

I changed it so you not needing to create a new array each time to check the length, this would just go a get the last object in the array and check the `statusCode`.

```javascript
let bodyData = pm.response.json(),
    lastStatusObject = bodyData.statusHistory[bodyData.statusHistory.length -1]

if (lastStatusObject.statusCode !== "BOOKED" ){  
    setTimeout(()=>{},1000);
    postman.setNextRequest(pm.info.requestName);
} else if (lastStatusObject.statusCode === "BOOKED" ) {
    postman.setNextRequest(null);
}

```

Might be totally wrong and not what you’re looking for, I was just curious 😃

---

<div class="post-metadata">

**Author:** ![oasis](https://avatars.discourse-cdn.com/v4/letter/o/a698b9/32.png) [@oasis](https://community.postman.com/u/oasis)\
**Post date:** [November 11, 2020, 3:30pm UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/10 "2020-11-11T15:30:03Z")

</div>

No, that’s amazing @danny-dainton. It worked:) Thank you. Only issue is I can’t assert :-

`pm.expect(response.statusHistory[2].statusCode).to.eql("BOOKED");`

error –  
`TypeError: Cannot read property 'statusCode' of undefined`

The above assert gets executed before pm.setNextRequest() and of course it can’t find that key/value in the array when it’s run first time. So, is there anyway we could wait for the setNextRequest() to complete before I assert? I have read that setNextRequest() gets executed last always…

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 11, 2020, 4:01pm UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/11 "2020-11-11T16:01:14Z")

</div>

Not completely sure without having that in front of me to run but you could use [\_.get()](https://lodash.com/docs/4.17.15#get) to check to see if that object is present in the array, if it is…run the test.

```auto
if(_.get(response.statusHistory, '[2].statusCode') !== undefined) {
    pm.test("Status is BOOKED", () => {
        pm.expect(response.statusHistory[2].statusCode).to.eql("BOOKED")
    });
}

```

or you could add something different that runs a ‘skipped’ test unless the object is present 🤷‍♂️

```auto
let skipTest = _.get(response.statusHistory, '[2].statusCode') === undefined;

(skipTest ? pm.test.skip : pm.test)("Status is BOOKED", () => {
    pm.expect(response.statusHistory[2].statusCode).to.eql("BOOKED")
});

```

---

<div class="post-metadata">

**Author:** ![oasis](https://avatars.discourse-cdn.com/v4/letter/o/a698b9/32.png) [@oasis](https://community.postman.com/u/oasis)\
**Post date:** [November 11, 2020, 4:21pm UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/12 "2020-11-11T16:21:50Z")

</div>

That worked. Thanks again @danny-dainton. I have only been using Postman for last 30 days or so, so, unfortunately have lots of questions :). I appreciate the help

---

<div class="post-metadata">

**Author:** ![oasis](https://avatars.discourse-cdn.com/v4/letter/o/a698b9/32.png) [@oasis](https://community.postman.com/u/oasis)\
**Post date:** [November 11, 2020, 4:47pm UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/13 "2020-11-11T16:47:30Z")

</div>

@danny-dainton, Just one more question out of curiosity on the first solution you gave. How does it iterate without a loop until it gets the BOOKED status. I can see it as shown below —

![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/5/f573838b28e5030cfd332a96168daa866698e62c.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 11, 2020, 5:45pm UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/14 "2020-11-11T17:45:35Z")

</div>

As you’re using the runner, the loop is coming from the `postman.setNextRequest(pm.info.requestName)` statement and that will keep going until you tell it to stop.

In this case, you’re setting the next request, to be the same request, so it’s just going to keep repeating that until the condition in the `if` statement is met.

Once the `else if` condition returns `true`, it goes down the other path and hits the `postman.setNextRequest(null)` which is the command to exit the workflow.

This would only loop if you’re using the Collection Runner to run the request.

There are lots of different ways/methods to loop through something, the `for` and `while` loops are the common ones but this wasn’t really needed here as the _looping_ action is being done by the internal Postman function.

Hope that makes sense. 😃

---

<div class="post-metadata">

**Author:** ![oasis](https://avatars.discourse-cdn.com/v4/letter/o/a698b9/32.png) [@oasis](https://community.postman.com/u/oasis)\
**Post date:** [November 11, 2020, 8:45pm UTC](https://community.postman.com/t/waiting-for-elements-in-the-response-goes-to-infinite-loop/17583/15 "2020-11-11T20:45:14Z")

</div>

Yep, that makes perfect sense. Didn’t realize `postman.setNextRequest(pm.info.requestName)`would loop. Great explanation. Thanks
