How I can set in pre-request script to sequence for requests?

Hello, guys! I have pre-request script: https://pastebin.com/VN5DzMAh . How to set initJobSchedulesTable and initJobsTable requests cause execute first? (first and second requests here)

Hi Andrew!

Before I dive a little deeper to help you out, can you please confirm whether or not I understand your question? It sounds like you want to know how to make sure the requests are sent in the following order:

  1. To the /operator/initJobsTable endpoint.
  2. And then to the /operator/initJobSchedulesTable endpoint.

And then the rest of the requests in your code can happen at any time (we don’t really care, as long as it’s after these first two requests)?

Yes , you are right. Example : 1 -> 2 -> (3 || 4 || 5 ||6). Or you can do 1->2->3->4->5->6 but it’s not necessary .

I used this solution, but it’s a cheat) :
setTimeout(function(){
pm.sendRequest(req1, function () {
});
},1000);

setTimeout(function(){
pm.sendRequest(req2, function () {
});
},2000);

1 Like

When I have a bunch of API requests that need to run before a specific request, I just make them their own request instead of using pm.sendRequest. I find it’s easier to order and separate concerns.

1 Like

There’s a couple of ways to call the requests in sequential order.

We can either call each request in a sequence via pm.sendRequest's callback functions…OR, if we want to avoid callback hell (lots of nested callbacks :cold_sweat:), we can convert pm.sendRequest into a promise and take advantage of async/await (which is a “–fancier syntax to handle multiple promises in synchronous code fashion.” via)

The thing is…thanks to @felipeplets’s post on StackOverflow, I learned that Postman blocks the subsequent pm.sendRequests we try to make. Because of this, for both of the implementations I’m about to share, I needed to add both a setTimeout & a clearTimeout to make sure the connection stays open while the async tasks are being completed.

Example 1: Callbacks

const interval = setTimeout(() => {}, 4000);

// First request we're making
pm.sendRequest("https://covid19.mathdro.id/api", (err, res) => {
    if (err) {
        console.log("Oh no. Couldn't complete the 1st request: ", err);
        clearTimeout(interval);
    } else {
        const url = res.json().confirmed.detail;
        console.log("Yay! Completed the 1st request: ", url);

        // Second request we're making
        pm.sendRequest(url, function (err, res) {
            console.log('Yay! Completed the 2nd request: ', res.json());

            /* You can nest as many more requests as you need to here! */

        });
        clearTimeout(interval);
    }
});

Example 2: Promises

// Turns pm.sendRequest into a promise
const reqPromise = (urlPath) => {
    return new Promise ((resolve, reject) => {
        pm.sendRequest(urlPath, function (err, response) {
            if (err) reject(err);
            resolve(response);
        });
    });
}

// Gets the url we need
const getCovidData = async () => {
    const request = await reqPromise("https://covid19.mathdro.id/api");
    const data = await request.json();
    return data;
}

// Uses the url we fetched to get something else
const getMoreAPIDataWithUrl = async (newUrl) => {
    const request = await reqPromise(newUrl);
    const data = await request.json();
    return data;
}

// Calls our requests in order
const callRequestsInOrder = async () => {
    const interval = setTimeout(() => {}, 4000);

    try {
        // First request we're making
        const covidData = await getCovidData();
        console.log('index.html 27 | covid Data', covidData);

        // Second request we're making
        const detailData = await getMoreAPIDataWithUrl(covidData.confirmed.detail);
        console.log('index.html 31 | detail Data', detailData);
        
        /* You can add as many more requests as you need to make here! */

        clearTimeout(interval);
    } catch(error) {
        console.log('caught the error: ', error);
        clearTimeout(interval);
    }
};

callRequestsInOrder();
4 Likes

Ooh, that sounds interesting. Do you mind sharing how you made your own request?

Haha, I just mean adding a new request to the collection. Right click the collection > New Request.

You can do pre and post processing with requests, so you can set up data however you need for your real test.

2 Likes

Haha, oh! Okay. That’s a really great idea! Thanks for sharing.

I thought you pulled in an external fetch library to create your own request instead of using pm.sendRequest :grimacing:.

2 Likes