# Pre-request async code doesn't wait for execution

**URL:** <https://community.postman.com/t/pre-request-async-code-doesnt-wait-for-execution/44366>\
**Category:** Help Hub\
**Tags:** pre-request, asynchronous, pre-request-script, asyncapi\
**Created:** [February 23, 2023, 6:49am UTC](https://community.postman.com/t/pre-request-async-code-doesnt-wait-for-execution/44366 "2023-02-23T06:49:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahussain.ned](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@ahussain.ned](https://community.postman.com/u/ahussain.ned)\
**Post date:** [February 23, 2023, 6:49am UTC](https://community.postman.com/t/pre-request-async-code-doesnt-wait-for-execution/44366/1 "2023-02-23T06:49:26Z")

</div>

Hi,

One of my requirement is to call an async login API as a pre-request script in Postman windows client. Through this code I want to set the collection variable (jwt-token). The problem is that Postman doesn’t wait for the completion of the pre-request call and hence the actual request is throwing an error of ‘Un-authorized access’.

Below is the code:

(async ()=\>{  
const loginRequest = {  
method: ‘POST’,  
url: pm.collectionVariables.get(“loginUrl”),  
header: {  
‘Content-Type’: ‘application/json’  
},  
body:{  
mode:‘raw’,  
raw: JSON.stringify({ email: “”, password: “”})  
}  
}  
await pm.sendRequest(loginRequest,(error, response)=\>{  
pm.collectionVariables.set(“jwt-token”, response.token)  
})  
})()

---

<div class="post-metadata">

**Author:** ![arlem](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/arlem/32/3078_2.png) [@arlem](https://community.postman.com/u/arlem)\
**Post date:** [February 23, 2023, 8:40am UTC](https://community.postman.com/t/pre-request-async-code-doesnt-wait-for-execution/44366/2 "2023-02-23T08:40:59Z")

</div>

Hey @ahussain.ned , welcome to the Postman community!

I’d recommend having a look at this collection, it has examples of different ways to handle async operations and making sure things execute before the request goes through:  
[https://www.postman.com/postman/workspace/postman-answers/documentation/3407886-220af2f6-63f2-4d84-903d-99e6e296a8c8](https://www.postman.com/postman/workspace/postman-answers/documentation/3407886-220af2f6-63f2-4d84-903d-99e6e296a8c8)

You can also fork the collection and try it out for yourself.

---

<div class="post-metadata">

**Author:** ![telecoms-candidate10](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@telecoms-candidate10](https://community.postman.com/u/telecoms-candidate10)\
**Post date:** [October 3, 2023, 2:07am UTC](https://community.postman.com/t/pre-request-async-code-doesnt-wait-for-execution/44366/3 "2023-10-03T02:07:43Z")

</div>

Late, but since no one actually answered your question I’ll go ahead as I just got done feeling your pain.

The problem here is for whatever reason `pm.sendRequest` returns a `function` rather than a `Promise`.

The execution environment _does_ have a scan that detects top-level `Promises` though, so the solution is to wrap your async work in a `Promise` and call the resolve function like you would a termination signal in older unit testing frameworks:

```auto
new Promise((res) => pm.sendRequest(
  {... your config },
  (err, res) => {
      ... your code
     res()
  }
))

```

The documentation, DX, ergonomics around scripting and automation in this product could definitely use some love.
