1

I am trying to figure out how to make the second Request run after the first request returned a response.I tried to search on google, but I'm kinda new to Postman and I'm not sure what should I look for. I tried to do something like:

pm.sendRequest(req1, function(err, res) {
   pm.sendRequest(req2, function(err, done)
)});

But It didnt work

while(documentLength>0)
{
 pm.sendRequest(listDocumentsRequest, function(err, res){
    pm.environment.set('dossierId',res.json().documentsList[index].subDossierId)
    pm.environment.set('documentId',res.json().documentsList[index].documentId)
});
 pm.sendRequest(getDocumentRequest);
    index++;
    documentLength--;
}

So I'm trying to make the first Request(listDocumentsRequest) then wait till I got an answer, then run the second request (getDocumentRequest) ,wait till I got an answer then move to the next iteration.

Do you guys have any idea?

Best Regards

Edited after Chilly answer

while(documentLength>0)
{
const interval = setTimeout(() => {}, Number.MAX_SAFE_INTEGER);
function resolvedPromise() {
    return new Promise((resolve, reject) => {
        pm.sendRequest(listDocumentsRequest, (err, res) => {
            if (err) {
                console.log(err);
                reject();
            } else {
                    pm.environment.set('dossierId',res.json().documentsList[index].subDossierId)
                    pm.environment.set('documentId',res.json().documentsList[index].documentId)
                resolve();
            }
        });
    });
}
resolvedPromise()
    .then(pm.request(getDocumentRequest))
    .then(() => clearTimeout(interval))
    .catch(err => {
        console.log(err);
        clearTimeout(interval);
    });
    index++;
    documentLength--;
}
Paracetamol12
  • 35
  • 1
  • 8

1 Answers1

-1
// Example with a plain string URL
pm.sendRequest('https://postman-echo.com/get', (error, response) => {
    if (error) {
        console.log(error);
    } else {
        // Example with a plain string URL
        pm.sendRequest('https://postman-echo.com/get/1', (error, response) => {
            if (error) {
                console.log(error);
            } else {
                console.log(response);
            }
        });
    }
});

You can simply chain the requests what is the error you are facing? in your case it looks like

while (documentLength > 0) {
    pm.sendRequest(listDocumentsRequest, function (err, res) {
        pm.environment.set('dossierId', res.json().documentsList[index].subDossierId)
        pm.environment.set('documentId', res.json().documentsList[index].documentId)
        pm.sendRequest(getDocumentRequest, function (err, res) {});
        index++;
        documentLength--;
    });

}
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Hello, Thank you for your answer, but it still doesn't work, but the problem isn't from the code itself, probably from Postman beacuse after I run the request with this code it crashes, but its ok, I found a workaround in my case. I made first request to run in pre-request script in a dummy request and the next request to run in Tests tab in the same Dummy Request. – Paracetamol12 Nov 20 '20 at 09:59