Socket hang up error

Hey Raje,

I think I understand why things were failing for you.

You mentioned using a for loop, which I missed.
Inside the for loop you are calling setTimeout() to initiate the performRequest() function.

Since setTimeout would return back immediately, all your requests would again be called pretty much simultaneously.

You should have something like the following to call your requests, instead of an explicit loop construct.

// I've put in some dummy data here
// in your case `requestList` would be the list of URLs you read from the response
let requestList = [
   "http://localhost?id=1",
   "http://localhost?id=2",
   "http://localhost?id=3",
   "http://localhost?id=4",
   "http://localhost?id=5",
   "http://localhost?id=6",
   "http://localhost?id=7",
   "http://localhost?id=8",
   "http://localhost?id=9",
   "http://localhost?id=10",
];

// We are setting a global index variable to keep track of requests being sent
let idx = 0;

// This function will execute the request to the URLs in requestList
function performRequest() {
    
    // URL to send the request to
    let url = requestList[idx];
    console.log("Calling:", url);

    // Send the actual request
    pm.sendRequest(url, (err, res) => {
        console.log("Finished:", requestList[idx]);
        
        // Check if requests are remaining and call the next one after 2 seconds delay.
        if( ++idx < requestList.length) {
            setTimeout(performRequest, 2000);
        } 
    });
}

// Start sending requests
performRequest();

Unfortunately, async-await and promise aren’t yet supported within Postman sandbox, else we could have a cleaner solution. Till then, the above solution should work!

Just came to know about a hack to make the async/await work in Postman sandbox, thanks to @kevin.swiber :raised_hands:
Linking to their solution: Executing sync requests programmatically from a pre-request or test scripts