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â.
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:
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.