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

I have the following setup:

Pre-Request script:
if (pm.environment.get === null){
pm.environment.set(“userEmail”, “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

but the request is sent as if it was not set:
Screen Shot 2021-01-28 at 6.30.10 PM

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!

Hey @abhathway! Welcome to the community :wave:

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

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

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

Hi @abhathway,

I think you are missing the open/close parenthesis and argument in your get statement.
image

Thanks and regards,
Allen

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