# How can I force pre-request scripts to execute synchronously when setting a variable via pre-request that the request will use?

**URL:** <https://community.postman.com/t/how-can-i-force-pre-request-scripts-to-execute-synchronously-when-setting-a-variable-via-pre-request-that-the-request-will-use/19676>\
**Category:** Help Hub\
**Tags:** pre-request, pre-request-script\
**Created:** [January 29, 2021, 2:30am UTC](https://community.postman.com/t/how-can-i-force-pre-request-scripts-to-execute-synchronously-when-setting-a-variable-via-pre-request-that-the-request-will-use/19676 "2021-01-29T02:30:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![award-bounteous](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@award-bounteous](https://community.postman.com/u/award-bounteous)\
**Post date:** [January 29, 2021, 2:30am UTC](https://community.postman.com/t/how-can-i-force-pre-request-scripts-to-execute-synchronously-when-setting-a-variable-via-pre-request-that-the-request-will-use/19676/1 "2021-01-29T02:30:54Z")

</div>

I have the following setup:

Pre-Request script:  
if (pm.environment.get === null){  
pm.environment.set(“userEmail”, “[some@email.com](mailto:some@email.com)”);  
};

And the request body:  
{  
“login”: “{{userEmail}}”,  
“password”: “{{userPW}}”  
}

To reproduce, create userEmail variable and userPW variable, then leave userEmail values blank in the environment variables.

The goal is to set it to a default email address in the event that the environment variable value has been deleted.

When I execute this, I’m being told that the email cannot be blank by the API.

The variable is set,  
 ![Screen Shot 2021-01-28 at 6.29.47 PM](https://us1.discourse-cdn.com/getpostman/original/3X/9/2/92285ccee428f7fae0a0028f0182fd3ed222234d.png)

but the request is sent as if it was not set:  
 ![Screen Shot 2021-01-28 at 6.30.10 PM](https://us1.discourse-cdn.com/getpostman/original/3X/e/4/e40457c83c648a34fde9f740e7c566ab5c930414.png)

What am I doing wrong? I have tried both a function and non-function format. No improvement. To make matters worse, this was working for me just the other day (1-2 days ago) when I first implemented it and I tested it multiple times with success.

Edit: I’ve also tried using a setTimeout(1000); with no success. I might be using it wrong, though. Not really a programmer. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![taehoshino](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/taehoshino/32/6608_2.png) [@taehoshino](https://community.postman.com/u/taehoshino)\
**Post date:** [January 29, 2021, 4:30am UTC](https://community.postman.com/t/how-can-i-force-pre-request-scripts-to-execute-synchronously-when-setting-a-variable-via-pre-request-that-the-request-will-use/19676/2 "2021-01-29T04:30:28Z")

</div>

Hey @award-bounteous! Welcome to the community 👋

I tried to reproduce your situation and the following code worked for me 🙂

```
if (!pm.environment.get('userEmail')) {
pm.environment.set('userEmail', "test@example.com")
}

```

I hope this helps!  
Let me know if you have further questions 😉

---

<div class="post-metadata">

**Author:** ![allen.vargas](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@allen.vargas](https://community.postman.com/u/allen.vargas)\
**Post date:** [January 29, 2021, 5:21am UTC](https://community.postman.com/t/how-can-i-force-pre-request-scripts-to-execute-synchronously-when-setting-a-variable-via-pre-request-that-the-request-will-use/19676/3 "2021-01-29T05:21:32Z")

</div>

Hi @award-bounteous,

I think you are missing the open/close parenthesis and argument in your `get` statement.  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/9/7936de0a43a8cf03543a4091142a3fa204ec1fe7.png)

Thanks and regards,  
Allen

---

<div class="post-metadata">

**Author:** ![award-bounteous](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@award-bounteous](https://community.postman.com/u/award-bounteous)\
**Post date:** [January 29, 2021, 6:04pm UTC](https://community.postman.com/t/how-can-i-force-pre-request-scripts-to-execute-synchronously-when-setting-a-variable-via-pre-request-that-the-request-will-use/19676/4 "2021-01-29T18:04:23Z")

</div>

Oh my gosh! I can’t believe I missed something so obviously wrong. I have no excuses. Thanks again!
