Collection variables don't want to be set after an `await` call it seems

Greetings!

I have learned a lot about how awesome Postman is over the past few days, however, I am now stuck with an issue I can not resolve. I would greatly appreciate a nudge in the right direction?

Setup:
In a Pre-request Script I have a call to a function that returns a Promise wrapped around a pm.sendRequest. In the result I receive the response object, pull a value off and attempt to store it in a collection variable. In the Request URI I then echo/use said collection variable.

Problem:
The collection variable just won’t save (well, if I do a .get straight after the .set it is there). I’ve added in console.logs to try and see whether everything is happening in order and it appears that it is. However, the Request URI still doesn’t contain/echo the collection variable specified.

Code:
The Pre-request Script…

(async () => {
    try {
        const subscription = await utils.createSubscription();
        pm.environment.set('subscription_id', subscription.id);
    } catch (error) {
        console.error(error);
    }
})();

The createSubscription function…

const createSubscription = () => {
        return new Promise((resolve, reject) => {
            pm.sendRequest({
                url: `${api_endpoint}/subscriptions`,
                method: 'POST',
                header: {
                    'Content-Type': 'application/json',
                    Authorization: pm.globals.get('jwt_token'),
                },
                body: {
                    mode: 'raw',
                    raw: JSON.stringify({
                        param1: "123",
                        param2: "123",
                        param3: "123"
                    })
                }
            }, (err, res) => {
                if (err) reject(err);
                else {
                        resolve(res.json().id);
                    }
                }
            });
        });
    };

Could it be a scope issue? I have passed pm into the anonymous function with no luck. Is it an async timing issue?

Thank you for your consideration :slight_smile:

Hi @lukefilewalker ,
I tried the exact same thing that you described in your setup, here’s a working solution - https://www.postman.com/mdhananii/workspace/meena-s-public-workspace/request/582779-e567ca70-b911-46d6-84ca-23d37d209f81

What did I learn? :nerd_face:

  • In the first part where you use the async function and call it, apparently, I received an error that said I’m not allowed to do it directly, so I have extracted that bit into a variable and then called the function.
  • In case you try to log something right after you call the async function, you might want to check the sequence in which the calls get executed since it’s async.
  • If you’re looking for the variable that you set programmatically to appear in the environments UI, I don’t see that happening, but I’m still able to do a get call on the variable right after setting it.

Please let me know if the solution in my workspace helps. :crossed_fingers: Feel free to fork it :smiley:

P.S: Sharing a solution using a public workspace was @kitty-garrison 's idea.

1 Like