pm.sendRequest() execution order

I am having multiple pm.sendRequest() that are being kept in pre-request tab of a request.
My concern is that not able to send the requests as per order.
For example.Login should be first,then job creation,then task creaion like that…
When tried to send a request which contains all the pm.sendRequest(),login ,job creation,task creation request are getting collasped in execution order.

Is there way to keep run the requests in a order…

Hey @spacecraft-technol17

The requests are async and could be sent in any order.

This is a frequently asked question and @singhsivcan did some great work with providing a potential solution for your workflow:

There’s also an example of these in this Public Workspace

https://www.postman.com/postman/workspace/postman-answers/collection/3407886-220af2f6-63f2-4d84-903d-99e6e296a8c8?action=share&source=copy-link&creator=8213448

1 Like

Thanks for your reply…Now i am happy as my issues are solved

1 Like

Hi Danny, isn’t that a workaround from before the promise support was fixed in Postman. (Sometime in 2023).

I did try searching for a new example but couldn’t find one.

So here is my effort.

First is a showcase of the issue using a loop to send multiple requests to Postman Echo.

We are updating the body of the request on each iteration to include the iteration number.

for (let i = 1; i < 11; i++) {

    let testValue = `request${i}`

    let request = {
        url: 'https://postman-echo.com/get?test=' + testValue,
        method: 'GET',
    }

    pm.sendRequest(request, (err, res) => {
        if (err) {
            console.log(err);
        }
        pm.test(`Request ${i} - status code is 200`, () => {
            pm.expect(res).to.have.status(200);
            let resJson = res.json();
            console.log(resJson.args.test);
        });
    });

The console logs then show that the requests (and tests) are not going sequentially due to the async nature of sendRequest().

image

Therefore, we are going to update our code to use Promises and awaits.

The await will stop processing any more code until the promise has been resolved. Please note: awaits only work in async functions.

const sendRequest = (req) => {
    return new Promise((resolve, reject) => {
        pm.sendRequest(req, (err, res) => {
            if (err) {
                console.log(err);
                return reject(err);
            }
            resolve(res);
        });
    });
};

(async function main() {

    for (let i = 1; i < 11; i++) {

        let testValue = `request${i}`

        let request = {
            url: 'https://postman-echo.com/get?test=' +testValue,
            method: 'GET',
        }

        const response = await sendRequest(request);

        pm.test(`Request ${i} - status code is 200`, () => {
            pm.expect(response).to.have.status(200);
            let resJson = response.json();
            console.log(resJson.args.test);
        });

    };
    
})();

With the console logs and tests now being in the expected order.

image

You’re probably referring to this one right?

If either of the solutions work for @spacecraft-technol17, it’s all good and gets them unblocked.

1 Like

Not exactly that one, but its the same topic and I’ve amended my code slightly based on that topic.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.