I’m trying to trigger a “pm.sendRequest” inside a for loop and the request is getting sent only after the iteration is completed, though I have kept the request inside the loop.
My Test look like,
for (i=0;i<5;i++)
{
const requestObject = {
url: 'https://reqres.in/api/users?page=2',
method: 'GET',
header: 'Content-Type:application/json',
}
console.log(i)
pm.sendRequest(requestObject, (err, res) => {
console.log(i)
})}
The "i" which is getting printed inside the pm.sendRequest is alwas printed as 5.
Could you please help me in resolving this issue.
I know what you mean
That’s because the pm.sendRequest is asynchronous function and by the time res is returned from pm.sendRequest call, the value of i is already proceeded to the last value.
If you want to show value of i corresponding to each request, you can use Promise, e.g.:
Can you please give an example when I want the FOR loop to wait for “sendReq” before sending the next “sendReq”?
In this case it will wait before “console.log”, but the 2nd request may be still executed before “console.log” of the 1st request.