I was working with a colleague and we were trying to achieve something by chaining multiple requests as a pre-step before the actual request happened.
Use case:
We wanted to call 3-4 specific endpoints in a specific order and gather all the required data before the actual request was sent out.
One way was to do it by creating multiple requests and using the collection runner. But the other way was to make use of pm.sendRequest
, however, there’s no guarantee in which order multiple of pm.sendRequest
would execute during the execution phase of the script.
So, I ended up creating 2 operations of the famous async library (probably will work on adding more) in the most naive way as possible, but it works so… ¯\(ツ)/¯
This is just an example to showcase what’s do-able using scripts, and how a lot of things can be achieved with chaining everything in just one request.
Reference:
See the pre-request scripts of each request in the following collection and fork the collection if you want to try it out:
https://www.postman.com/postman/workspace/postman-answers/collection/3407886-220af2f6-63f2-4d84-903d-99e6e296a8c8?ctx=documentation
Example code snippet:
// Expected output in console: 3, 1, 2
asyncSeries([
(cb) => pm.sendRequest('https://postman-echo.com/delay/3', (err, res) => {
console.log(res.json().delay);
cb(err, res);
}),
(cb) => pm.sendRequest('https://postman-echo.com/delay/1', (err, res) => {
console.log(res.json().delay);
cb(err, res);
}),
(cb) => pm.sendRequest('https://postman-echo.com/delay/2', (err, res) => {
console.log(res.json().delay);
cb(err, res);
})
], (err, res) => {
console.log('Series operations resolved', err, res);
});
Screenshot: