I need to use some async code in my Postman test.
As it's a complex scenario I've reproduced the scenario in a very simple test with the following code:
let promiseNumber = 0;
function resolvedPromise() {
return new Promise((resolve, reject) => {
pm.sendRequest('https://postman-echo.com/get', (err, res) => {
if (err) {
console.log(err);
reject();
} else {
console.log(`Resolved promise ${++promiseNumber}`);
resolve();
}
});
});
}
resolvedPromise()
.then(resolvedPromise)
.then(resolvedPromise)
.catch(err => console.log(err));
The expected result on the console would be:
Resolved promise 1
Resolved promise 2
Resolved promise 3
But instead I receive:
Resolved promise 1
Is there a way to make Promises or async code available at Postman?
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
A promise is an object returned by an asynchronous function, which represents the current state of the operation. At the time the promise is returned to the caller, the operation often isn't finished, but the promise object provides methods to handle the eventual success or failure of the operation.
The Postman Sandbox detects open timeouts and intervals and awaits execution until they're all cleared/resolved. If we use a dummy Interval object, we can use async/await to have a cleaner method of chaining asynchronous calls.
Promises Can Be Paused Just because you are already executing inside a then() function, doesn't mean you can't pause it to complete something else first. To pause the current promise, or to have it wait for the completion of another promise, simply return another promise from within then() .
UPDATE: The original solution used 2147483647 as timeout value, now it was refactored to use Number.MAX_SAFE_INTEGER as suggested in the comments.
I did some more tests and realize that it always stop working after I use pm.sendRequest. If I try to resolve the Promise it works.
Seems a known bug looking at this thread.
The workaround for it would be only leave an open timeout while processing the code. Just ensure all possible paths clear the timeout or the call will hang for 300000 years 😀
// This timeout ensure that postman will not close the connection before completing async tasks.
// - it must be cleared once all tasks are completed or it will hang
const interval = setTimeout(() => {}, Number.MAX_SAFE_INTEGER);
let promiseNumber = 0;
function resolvedPromise() {
return new Promise((resolve, reject) => {
pm.sendRequest('https://postman-echo.com/get', (err, res) => {
if (err) {
console.log(err);
reject();
} else {
console.log(`Resolved promise ${++promiseNumber}`);
resolve();
}
});
});
}
resolvedPromise()
.then(resolvedPromise)
.then(resolvedPromise)
.then(() => clearTimeout(interval))
.catch(err => {
console.log(err);
clearTimeout(interval);
});
Now it prints the expected result:
Resolved promise 1
Resolved promise 2
Resolved promise 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With