# Sending multiple API requests every 2, 4, 6, and 8 seconds in a loop

**URL:** <https://community.postman.com/t/sending-multiple-api-requests-every-2-4-6-and-8-seconds-in-a-loop/38139>\
**Category:** Help Hub\
**Created:** [August 12, 2022, 1:17am UTC](https://community.postman.com/t/sending-multiple-api-requests-every-2-4-6-and-8-seconds-in-a-loop/38139 "2022-08-12T01:17:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [August 12, 2022, 1:17am UTC](https://community.postman.com/t/sending-multiple-api-requests-every-2-4-6-and-8-seconds-in-a-loop/38139/1 "2022-08-12T01:17:05Z")

</div>

Hi,

I want to send multiple API requests every 2, 4, 6, and 8 seconds in a loop as follows…

Loop Start  
API Request 1  
Delay 2 seconds  
API Request 2  
Delay 2 seconds  
API Request 3  
Delay 2 seconds  
Loop End

Then I want to replace the 2 seconds above with 4, then 6, then 8 seconds for each pass through the loop. Any ideas on the best way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![ianthepostmanaut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ianthepostmanaut/32/11109_2.png) [@ianthepostmanaut](https://community.postman.com/u/ianthepostmanaut)\
**Post date:** [September 10, 2022, 7:12am UTC](https://community.postman.com/t/sending-multiple-api-requests-every-2-4-6-and-8-seconds-in-a-loop/38139/2 "2022-09-10T07:12:33Z")

</div>

This will be a little bit tricky since JavaScript doesn’t really have a “sleep” API to pause for an exact amount of time when running procedural code. Also, if your test code is generating new requests, those are done in Promises and there’s no guarantee when they would return, so you would have to write out the test in a nested format like this:

```auto
// create a folder setup like this:
+ Collection Name
  |
  +-- Subfolder
         |
         +--- request

```

Then, in the “Collection” folder’s pre-request script, set an environment variable like this:

```auto
pm.environment.set('delay-time', 2)

```

Then, in your “request” test script, do something like this:

```auto
var delay = pm.environment.get('delay-time');

pm.sendRequest("https://localhost/request1", function (err, response) {
    // handle the response here
    // figure out some way to sleep for "DELAY" seconds

    pm.sendRequest("https://localhost/request2", function (err, response) {
        // handle the response here
        // figure out some way to sleep for "DELAY" seconds
        pm.sendRequest("https://localhost/request3", function (err, response) {
            // handle the response here
            // figure out some way to sleep for "DELAY" seconds
            if (delay * 2 < 64) {
                pm.environment.set('delay-time', delay * 2)
                pm.setNextRequest('request')
            } else {
                pm.setNextRequest(null)
            } 
        });
    });

});

```

Because `pm.sendRequest()` is asynchronous, you can’t simply do them “in order” as they would all get called around the same time and you wouldn’t know which one would respond first … nesting them in the structure I’ve outlined above should at least ensure that request 1 will be called, and then progress to 2, then to 3, at which point the code will do your exponential back-off and retry the API endpoints again by called `pm.setNextRequest` … once you hit a threshold of some sort, in this case I say less than 64, then you call `pm.setNextRequest(null)` which will stop the looping.

Another approach would be to make this structure:

```auto
// create a folder setup like this:
+ Collection Name
  |
  +-- Subfolder
         |
         +--- request1
         +--- request2
         +--- request3

```

… and the test code for each of those three requests would be something like this maybe:

```auto
// request 1's test code:
var delay = pm.environment.get('delay-time');
// figure out how to "sleep" for that many seconds
pm.setNextRequest('request2')

```

then

```auto
// request 2's test code:
var delay = pm.environment.get('delay-time');
// figure out how to "sleep" for that many seconds
pm.setNextRequest('request3')

```

then

```auto
// request 3's test code:
var delay = pm.environment.get('delay-time');
// figure out how to "sleep" for that many seconds

if (delay * 2 < 64) {
  pm.environment.set('delay-time', delay * 2)
  pm.setNextRequest('request1') // start over
} else {
  pm.setNextRequest(null)
}

```

---

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [September 16, 2022, 5:38pm UTC](https://community.postman.com/t/sending-multiple-api-requests-every-2-4-6-and-8-seconds-in-a-loop/38139/3 "2022-09-16T17:38:01Z")

</div>

Hey @ianthepostmanaut - many thanks for this super informative and detailed response!

Before I go down this road, I wondered if you or anyone in the community had thoughts on doing what I’m trying to accomplish here via Postman versus Apache JMeter?

A third option is a Powershell script I have developed that basically accomplishes the same thing.

I use Postman (great tool btw!) for single session API simulations and its works great for me for that.

I was asking about this question on this thread because I may need to get into load testing where I have multiple API sessions running concurrently. I’m that impressed with Postman that I thought I’d bring all my testing under the one roof here, but I’m not sure if Powershell scripts and/or JMeter are worth the learning curve effort or better suited to my purpose? Any input on this from you and/or the community much appreciated.

---

<div class="post-metadata">

**Author:** ![iandouglas736](https://avatars.discourse-cdn.com/v4/letter/i/ea666f/32.png) [@iandouglas736](https://community.postman.com/u/iandouglas736)\
**Post date:** [September 23, 2022, 3:55pm UTC](https://community.postman.com/t/sending-multiple-api-requests-every-2-4-6-and-8-seconds-in-a-loop/38139/4 "2022-09-23T15:55:51Z")

</div>

If you have access to a shell environment then I would just use Newman and it’s collection runner where you could control the timing yourself. If all of your requests are absolutely uniquely named, a fun hidden feature of Newman is that you can pass a single request as the “folder” argument. Your shell script can then control more precise timing, stop any time it sees an error, etc.

---

<div class="post-metadata">

**Author:** ![ianthepostmanaut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ianthepostmanaut/32/11109_2.png) [@ianthepostmanaut](https://community.postman.com/u/ianthepostmanaut)\
**Post date:** [September 26, 2022, 3:35pm UTC](https://community.postman.com/t/sending-multiple-api-requests-every-2-4-6-and-8-seconds-in-a-loop/38139/5 "2022-09-26T15:35:26Z")

</div>

hey @teamgyst that previous reply was also from me, I was logged in on a different account on my phone. 🙂

I understand what you’re trying to do regarding load testing. You might want to see about using a proper benchmarking tool like [Apache Benchmark](https://httpd.apache.org/docs/2.4/programs/ab.html) where it can give you full load results, timings, etc, if you’re looking to do something like what you’ve described.

---

<div class="post-metadata">

**Author:** ![teamgyst](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@teamgyst](https://community.postman.com/u/teamgyst)\
**Post date:** [November 15, 2022, 6:50pm UTC](https://community.postman.com/t/sending-multiple-api-requests-every-2-4-6-and-8-seconds-in-a-loop/38139/6 "2022-11-15T18:50:46Z")

</div>

Hi @ianthepostmanaut - sincere apologies for the really delayed reply, super busy here. I really like this “This especially shows you how many requests per second your Apache installation is capable of serving.”. Will investigate further. Many thanks!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [December 12, 2024, 12:52pm UTC](https://community.postman.com/t/sending-multiple-api-requests-every-2-4-6-and-8-seconds-in-a-loop/38139/7 "2024-12-12T12:52:18Z")

</div>

Closed on December 12, 2024, 12:52pm UTC

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