Javascript and node now has native support for Promises. I was hoping we could use these in Postman and newman as well to chain async sendRequest calls.
Although declaring and using new Promise works but chaining does not work.
For example look at the code below (put this in test script of any request)
This essentially adds a timeout that’s closed when all promises are resolved. This makes the Postman script module to wait till that time before proceeding to send the request.
Postman Tests with chained promises is executing only the first promise but not chained promises. I am not sure whats wrong with this code but it is executing only 1st promise and ignoring the rest.
I have tried based on the above solution but didn’t work. Kindly help me in this regard.
It doesn’t seem to work in the newer versions of postman. I have tried numerous iterations of this solution to no luck. I have found that going to a lovely (/sarcasm) callback approach works…
let promiseNumber = 0;
function resolvedPromise(cb) {
pm.sendRequest('https://postman-echo.com/get', (err, res) => {
if (err) {
console.log(err);
cb(err);
} else {
console.log(`Resolved promise ${++promiseNumber}`);
cb(res.json());
}
});
}
resolvedPromise(()=>resolvedPromise(()=> resolvedPromise(()=>{})))
Any update on this? Is it confirmed that the setTimeout technique for chaining promises with pm.sendRequest is NOT working in the latest version of Postman sandbox?
I’m on 7.22.1 and I also cannot seem to get the setTimeout work around to work either.
There is any update on this issue?
I’ve implemented the code below to asynchronously get domain values from my API. The promises are ok and run well, but pm.globals.set(“domains”, values) don’t persist “values” content. I’ve tried using environment, but still, “values” are never saved to the “domains” variable.
But the content is console logged perfectly. Can anyone figure out what’s going wrong?
Has anyone made a formal bug-report, i.e. raised an issue, about this in GitHub?
If the answer is yes, what is the link to that issue?
BTW, I am currently inserting: const lock = setTimeout(() => {}, 43210);
as the first row in my code, and later: clearTimeout(lock); // Unlock the timeout.
as the first row in the final callback.
It works great as a workaround, so thanks for suggestiing it!
I think this post could help in resolving this issue of running chained requests in a series (wouldn’t really call it synchronously since requests are actually async in nature ): Async Operations!
This can be one temporary solution till promises are natively supported in Postman.