# Sandbox Script Errors

**URL:** <https://community.postman.com/t/sandbox-script-errors/45143>\
**Category:** Help Hub\
**Tags:** scripts\
**Created:** [March 15, 2023, 8:26am UTC](https://community.postman.com/t/sandbox-script-errors/45143 "2023-03-15T08:26:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tolik87](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@tolik87](https://community.postman.com/u/tolik87)\
**Post date:** [March 15, 2023, 8:26am UTC](https://community.postman.com/t/sandbox-script-errors/45143/1 "2023-03-15T08:26:05Z")

</div>

Hi all,  
Glad to finally be here. We have the following API problem(ofc beautified) 😃  
Post asset creates an asset, let us assume the following:

“id”: “51fc4807-d3ed-4b6e-b1f8-ee3eb2e18a96”,  
“name”: “Tolik87”,  
“description”: “APIDescription”,  
“status”: “DAMNED”,

and after that another POST that changes the status of the asset to NOT-DAMNED (it is a job that takes around 3 mins). I want to be able to poll it for when the status becomes NOT-DAMNED, so hitting the API every 30 sec to check if the status is NOT-DAMNED  
This should do the trick:  
const response = pm.response.json();

if (response.status !== “NOT-DAMNED”) {  
setTimeout(() =\> {}, 30000)  
postman.setNextRequest(pm.info.requestId)  
}

However I am looking for something to break my loop if it never becomes “NOT-DAMNED”. Anyone can help with that. Tried to save counter in an environment variable but maybe am too stupid to write that script by myself ☹

---

<div class="post-metadata">

**Author:** ![tolik87](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@tolik87](https://community.postman.com/u/tolik87)\
**Post date:** [March 15, 2023, 9:35am UTC](https://community.postman.com/t/sandbox-script-errors/45143/2 "2023-03-15T09:35:39Z")

</div>

setTimeout(() =\> {}, 30000)  
const response = pm.response.json()  
if (!pm.environment.has(“counter”)) {  
console.log(“set counter to 0”);  
pm.environment.set(“counter”, 0);  
}

let currentCount = parseInt(pm.environment.get(“counter”));  
console.log(currentCount)

if (currentCount \< 10) {  
if (response.status === “NOT-DAMNED”) {  
postman.setNextRequest(“Next Request”);  
}  
currentCount++;  
pm.environment.set(“counter”, currentCount);  
postman.setNextRequest(pm.info.requestId);  
}

I came till here but that does not work properly. Added setTimeOut and now when I log count, it stays the same ☹

---

<div class="post-metadata">

**Author:** ![ddegey](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ddegey/32/27840_2.png) [@ddegey](https://community.postman.com/u/ddegey)\
**Post date:** [March 13, 2024, 9:06pm UTC](https://community.postman.com/t/sandbox-script-errors/45143/3 "2024-03-13T21:06:19Z")

</div>

> [@tolik87](#):
>
> This should do the trick:  
> **const response = pm.response.json();**
> 
> if (response.status !== “NOT-DAMNED”) {  
> setTimeout(() =\> {}, 30000)  
> postman.setNextRequest(pm.info.requestId)  
> }

Hi @tolik87

If I’m not mistaken, “status” is not a “response” property but a “pm.response” property.

I’ve followed an Udemy course about postman where the instructor always use the same `const response = pm.response.json();`.  
I think this is a very bad practice because it is not a response but the data of a reponse.  
I recommand you to change to “respData” or “jsonData”…  
(“data” seems to be used by postman when you use a csv file, and represents a line, so, I don’t use it)

Hope this helps…

Best

Didier

EDIT: this post was in the top of the list, I did not see it was one year old… maybe it could help someone else 🙂

---

<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:** [March 14, 2024, 12:29pm UTC](https://community.postman.com/t/sandbox-script-errors/45143/4 "2024-03-14T12:29:38Z")

</div>

@ddegey

response.status is perfectly valid consider the original posters example response.

Status is a key in that response.

Not to be confused with the status on a assertion which is a function from Postman to help assert status codes.

`pm.expect(response).to.have.status(200)`

Two completely separate things.

To answer the original posters question.

Indefinite loops are a bad thing, so you would check for the NON-DAMNED status and a configurable counter object with the counter being the trigger that prevents the indefinite loop.

I can create an example, but as this is over a year old, I’m only going to do that if someone wants it (or I’ll just point to relevant questions on Stack Overflow as there are a bunch of questions related to best practice for loops on there).

---

<div class="post-metadata">

**Author:** ![ddegey](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ddegey/32/27840_2.png) [@ddegey](https://community.postman.com/u/ddegey)\
**Post date:** [March 14, 2024, 5:31pm UTC](https://community.postman.com/t/sandbox-script-errors/45143/5 "2024-03-14T17:31:19Z")

</div>

@michaelderekjones  
Oh yes, bien vu…  
I’ve missed that…  
Pfff how I’ve associated status and response without thinking… 😅

Thanks

Didier
