# Can I use pm.sendRequest() to send requests synchronously?

**URL:** <https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225>\
**Category:** Help Hub\
**Created:** [November 14, 2017, 3:12pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225 "2017-11-14T15:12:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![joss.lamberti](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@joss.lamberti](https://community.postman.com/u/joss.lamberti)\
**Post date:** [November 14, 2017, 3:12pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/1 "2017-11-14T15:12:12Z")

</div>

In the context of [http://blog.getpostman.com/2017/10/03/send-asynchronous-requests-with-postmans-pm-api/](http://blog.getpostman.com/2017/10/03/send-asynchronous-requests-with-postmans-pm-api/), I’d like to send 2 requests SYNCHRONOUSLY (and NOT Asynchronously) from a pre-request script.

One of the requests resets my auth token and the other fetches some data needed for the main request I want to send.  
The issue is that the data fetching request also needs that auth token to be successful.

Can I write my pre-request script in a way so that my data fetching request always runs AFTER my auth token request?

The code looks like this:

```auto
eval(pm.globals.get("adminLogin"))(); //sets authorizationToken

pm.sendRequest({
    url: pm.environment.get("host") + "/" + pm.environment.get("apiPath") + "/card/" + pm.environment.get("card1Id"),
    method: "GET",
    header: {
        "content-type": "application/json",
        "accept": "application/json",
        "authorization": pm.environment.get("authorizationToken")
    },
    body: {}
}, function (err, res) {
    pm.environment.set("txnExpiryDate", res.json().expiryDate.slice(0,2) + "" + res.json().expiryDate.slice(3, 5));
    pm.environment.set("txnCardNumber", res.json().cardNumber);
});

```

Thanks!

---

<div class="post-metadata">

**Author:** ![luisospina](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@luisospina](https://community.postman.com/u/luisospina)\
**Post date:** [November 28, 2017, 4:00pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/2 "2017-11-28T16:00:14Z")

</div>

Did you find any solution to do this synchronously?

found this [https://medium.com/@MrPonath/postman-pre-request-scripts-81920a78d0fc](https://medium.com/@MrPonath/postman-pre-request-scripts-81920a78d0fc) but they are not using the native pm.sendRequest.

---

<div class="post-metadata">

**Author:** ![joss.lamberti](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@joss.lamberti](https://community.postman.com/u/joss.lamberti)\
**Post date:** [November 28, 2017, 4:15pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/3 "2017-11-28T16:15:10Z")

</div>

No solution yet. So far, I’m trying to have 1 request only in the pre-request script, so for the example above, I make sure I’m logged in already.

---

<div class="post-metadata">

**Author:** ![kamalaknn](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kamalaknn/32/93_2.png) [@kamalaknn](https://community.postman.com/u/kamalaknn)\
**Post date:** [November 29, 2017, 7:34am UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/4 "2017-11-29T07:34:46Z")

</div>

The `pm.sendRequest`s are asynchronous in nature.

In the following snippet,

```auto
pm.sendRequest(req1, done);
pm.sendRequest(req2, done);

```

both `req1` and `req2` are sent out immediately without waiting for them to complete. In order to make `req2` wait for `req1` you have to nest `pm.sendRequest(req2)` inside the callback of `req1`. It will be something like,

```auto
pm.sendRequest(req1, function () {
  pm.sendRequest(req2, done);
});

```

Although this solves your problem, it can get unreadable and complex over time. I’m interested in finding alternatives to solve this. Have you tried seeting the three requests in order in a collection and running them?

---

<div class="post-metadata">

**Author:** ![joss.lamberti](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@joss.lamberti](https://community.postman.com/u/joss.lamberti)\
**Post date:** [November 29, 2017, 9:49am UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/5 "2017-11-29T09:49:48Z")

</div>

Thanks for the nested solution. Agreed, it might be quickly unreadable, unless perhaps if the nested request is done with the code in a variable, e.g.:

```auto
eval(pm.globals.get("customerLogin"))();

```

And yes, I have been using separate requests organised in collections and folders but I quickly reached in excess of 1800 requests for a single API so I’ve been trying to find ways to reduce that number. Including some requests in the pre-request scripts, especially authentication related ones, helps making things a bit easier to navigate through.

EDIT: oh, that bit was already in my example…

---

<div class="post-metadata">

**Author:** ![kamalaknn](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kamalaknn/32/93_2.png) [@kamalaknn](https://community.postman.com/u/kamalaknn)\
**Post date:** [November 29, 2017, 11:25am UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/6 "2017-11-29T11:25:21Z")

</div>

> I have been using separate requests organised in collections and folders but I quickly reached in excess of 1800 requests for a single API so I’ve been trying to find ways to reduce that number. Including some requests in the pre-request scripts, especially authentication related ones, helps making things a bit easier to navigate through.

We’ve been working on something that might help with that. We are introducing `scripts` and `auth` at collection/folder level. It is available for early access on our [Canary channel](https://www.getpostman.com/canary)(for linux and mac, coming out on windows soon).

---

<div class="post-metadata">

**Author:** ![haxel1975](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/haxel1975/32/595_2.png) [@haxel1975](https://community.postman.com/u/haxel1975)\
**Post date:** [May 26, 2018, 8:46am UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/7 "2018-05-26T08:46:32Z")

</div>

Is there any documentation how Postman Canary would support this as of today?

---

<div class="post-metadata">

**Author:** ![kamalaknn](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kamalaknn/32/93_2.png) [@kamalaknn](https://community.postman.com/u/kamalaknn)\
**Post date:** [May 26, 2018, 12:33pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/8 "2018-05-26T12:33:24Z")

</div>

We’ve added support for collection and folder level scripts and auth in our stable channel. Please update to the latest version and try it out. You can find the option to add scripts and auth in the `Edit Folder/Collection` option.

---

<div class="post-metadata">

**Author:** ![haxel1975](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/haxel1975/32/595_2.png) [@haxel1975](https://community.postman.com/u/haxel1975)\
**Post date:** [May 26, 2018, 5:34pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/9 "2018-05-26T17:34:31Z")

</div>

Thank you! So would this enable the option to send requests synchronously with pm.sendRequest()?

---

<div class="post-metadata">

**Author:** ![kamalaknn](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kamalaknn/32/93_2.png) [@kamalaknn](https://community.postman.com/u/kamalaknn)\
**Post date:** [May 29, 2018, 10:11am UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/10 "2018-05-29T10:11:13Z")

</div>

Nope. `pm.sendRequest`s are asynchronous always. However, you will have the option to group common scripts in folder/collection scripts in the latest version.

---

<div class="post-metadata">

**Author:** ![hock1szabol329](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@hock1szabol329](https://community.postman.com/u/hock1szabol329)\
**Post date:** [July 17, 2018, 2:16pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/11 "2018-07-17T14:16:35Z")

</div>

you can with javascript promise - await mechanism. even iterations is possible.  
//------------------sandos---------------------------  
function SendRequest(maxRequest) {  
return new Promise(resolve =\> {  
pm.sendRequest({ … },  
function(err, res) {  
var response = JSON.stringify(res);  
console.log("res: " + pinresponse);  
if (maxRequest\>1) {  
SendRequest(maxRequest-1);  
} else {  
resolve(1);  
}  
}  
);  
});  
}

async function SenReq(maxRequest) {  
return await SendRequest(maxRequest);  
}  
//Engineered with Sandos chan

---

<div class="post-metadata">

**Author:** ![eduardolago](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/eduardolago/32/926_2.png) [@eduardolago](https://community.postman.com/u/eduardolago)\
**Post date:** [September 17, 2018, 12:07pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/12 "2018-09-17T12:07:44Z")

</div>

Yes it is possible! For instance, I use this for retrieve a JS archive from a CDN, then make it available before the collection gets executed. So paste some similar snippet into your Pre-request Scripts:

```
var settings = {
    async: false,
    crossDomain: true,
    url: "https://myhost/mylib.js",
    method: 'GET',
};

pm.sendRequest(settings, (err, res) => { 
    eval(res.text()); 
});
```

---

<div class="post-metadata">

**Author:** ![tkeating](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/tkeating/32/668_2.png) [@tkeating](https://community.postman.com/u/tkeating)\
**Post date:** [October 31, 2018, 8:20pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/13 "2018-10-31T20:20:40Z")

</div>

That is not a valid Request object to pass to the pm.sendRequest function. The Request object is docmented here: [http://www.postmanlabs.com/postman-collection/Request.html](http://www.postmanlabs.com/postman-collection/Request.html), and it has neither “async” nor “crossDomain” properties. In short, you can’t do synchronous calls this way.

---

<div class="post-metadata">

**Author:** ![eduardolago](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/eduardolago/32/926_2.png) [@eduardolago](https://community.postman.com/u/eduardolago)\
**Post date:** [October 31, 2018, 10:19pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/14 "2018-10-31T22:19:23Z")

</div>

You are probably right. However, it s working for me so far! Magic? Older version? Which to.hear more details to have a deep understanding. Thanks for feedback

---

<div class="post-metadata">

**Author:** ![tkeating](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/tkeating/32/668_2.png) [@tkeating](https://community.postman.com/u/tkeating)\
**Post date:** [November 1, 2018, 7:00pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/15 "2018-11-01T19:00:25Z")

</div>

Older version, maybe. Trust me, I wanted that to work in the worst way!

For anyone else with this kind of need who stumbles across this thread, the way I ended up solving my particular use case was to break my request into two separate requests. In my case, the first request performs the API operation and the second fetches data from Couchbase to validate against. I attach the actual tests for the FIRST request to the test block of the SECOND request. I don’t use sendRequest at all, now.

In the OPs case, I would make the call to get the auth key from the first request and stash it in an environment variable, and then do the API request & associated tests in the second request.

---

<div class="post-metadata">

**Author:** ![roger7467](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@roger7467](https://community.postman.com/u/roger7467)\
**Post date:** [March 25, 2019, 9:52am UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/16 "2019-03-25T09:52:01Z")

</div>

I have a solution by using recursion.

```javascript
var lib = "";
var jsList = pm.iterationData.get("jsList").split("|");

if (!pm.variables.get("concatLib")){
    pm.variables.clear();
    libGetter();
}
else {
    eval(pm.variables.get("concatLib"));
    preRequestScript();
}

function libGetter(){
    pm.sendRequest({
        url: "localhost:8080/"+ jsList[0] +".js", 
    }, function (err, res) {
        if (err) { 
            console.log(err);
        }
        else {
            lib = lib.concat(res.text());
            jsList.shift();
            if(jsList[0]){
                libGetter();
            }
            else{
                pm.variables.set("concatLib", lib);
                eval(pm.variables.get("concatLib"));
                preRequestScript();
            }
        }
    });
}

```

---

<div class="post-metadata">

**Author:** ![andrespinillazamudio](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/andrespinillazamudio/32/2448_2.png) [@andrespinillazamudio](https://community.postman.com/u/andrespinillazamudio)\
**Post date:** [July 30, 2019, 7:32pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/17 "2019-07-30T19:32:08Z")

</div>

to be consistent with the postman workflow, you should have a pivot request with the aim of synchronously to call many times the other requests, the pivot could have a simple request like “[postman-echo.com/get](http://postman-echo.com/get)” in order to build the next request on the test script in Postman, the above must set the index value of the iteration to avoid infinity cycle, as show the next image

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/8/68c51bf955a491b9f52e73dd25774d61f1f53358.png)

the calculateFunds post will execute many times through of pivot request, and finally the calculateFunds test script will have to call pivot and eventually using the postman workflow, you don’t mind about the asynchronous callings.

---

<div class="post-metadata">

**Author:** ![jdang](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@jdang](https://community.postman.com/u/jdang)\
**Post date:** [December 4, 2019, 8:20pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/18 "2019-12-04T20:20:55Z")

</div>

Try something like this:

```javascript
sendPostRequest(12345)
    .then((data) => {
        console.log("data from postRequest ", data)
    })
    .catch((error) => {
		console.log("error: ", error)
	})

// function that retuns a Promise
function sendPostRequest(id) {

    return new Promise(
        resolve => {
            const postReq = {
                url: pm.environment.get('function-url'),
                method: 'POST',
                body: {
                    mode: 'raw',
                    raw: {
                        id: +id
                    }
                }
            };

            pm.sendRequest(postReq, function(err, res) {
                if (err) {
                    console.error(err);
                    return;
                }
                return resolve( res.json());
            });
        });
}

```

---

<div class="post-metadata">

**Author:** ![josephadams](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@josephadams](https://community.postman.com/u/josephadams)\
**Post date:** [June 21, 2020, 9:29pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/19 "2020-06-21T21:29:09Z")

</div>

@jdang thanks - I had to make a couple changes (just around the body part) but would it be possible to chain an additional get request?

The post requet returns “null” (which is entirely unhelpful) so I still need a get after this - but I admit I am struggling to see how

---

<div class="post-metadata">

**Author:** ![RameshKolli\_Postman](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@RameshKolli\_Postman](https://community.postman.com/u/RameshKolli_Postman)\
**Post date:** [September 10, 2020, 9:18pm UTC](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225/20 "2020-09-10T21:18:19Z")

</div>

You can try following to control the timing of your requests

setTimeout(function(){  
pm.sendRequest(req1, function () {  
});  
},1000);

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

setTimeout(function(){  
pm.sendRequest(req3, function () {  
});  
},3000);

[Next page](https://community.postman.com/t/can-i-use-pm-sendrequest-to-send-requests-synchronously/225.md?page=2)
