pm.sendRequest to send requests synchronously or with an interval?

I am trying to loop through an array gather the data ( ie account ids, that have a balance) and then send a request based on the data. (doing this sequence a few times 25…50 times ie that many elements in the array )

However when I run the script seems like all the pm.send requests are sent at the same time rather than sending the request and going through the loop again to get new data and sending the request again. Is there a way to make the pm.sendrequest (send the request) with a delay/interval rather than all at once ?

setTimeout(function(){}, 1000) after pm.sendRequest did not work either …

For reference here is the code being used … Any help would be appreciated … Thanks

for(var accIndex in data) {
    var source_account_id = data[accIndex].id;
    if(source_account_id != main_account_id){
        for(var product in data[accIndex].holdings){
            var product_details = data[accIndex].holdings[product];
            var product_id = product_details.product.id;
            var product_balance_quantity = product_details.balance.quantity;
            var product_balance = product_details.balance;
            if(product_balance_quantity > 0) {            
                let theUrl = protocol + '://' + uri + '/transactions'
                var body = {
                    transaction_type: "TRANSFER",
                    transfer_request:  {
                        source_account_id:data[accIndex].id,
                        destination_account_id: main_account_id,
                        product_id: product_id,
                        transfer_reason:"consolidation",
                        amount: product_balance,
                    }
                };
                const postRequest = {
                    url: theUrl,
                    method: 'POST',
                    header: {
                        'Content-Type': 'application/json',
                        'X-Foo': 'bar'
                    },
                    body: { 
                        mode: 'raw',
                        raw: JSON.stringify(body)
                    }
                };
                pm.sendRequest(postRequest, (error, response) => {
                    if (error) {
                        console.log(error);
                    } else {
                        console.log("Transfer initiated.");
                    }
                });
            }
            else {
                console.log("No  Balance to transfer" + data[accIndex].id + "with balance" + product_balance);
            }
        }
    }
    else {
        console.log("Skip parsing the main account" + data[accIndex].id);
    }
}

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

1 Like

pm.sendRequest works using call back , meaning the request is send in order but will be resolved in future. As you don’t have any dependency for keeping the order , meaning you don’t have to wait for the response to do the next step your code should work.

if you still want to make it print as per loop use something like:

(async()=>{

for (let i = 10; i >= 0; --i) {

    // Example with a plain string URL

    console.info("before : " + i)

    pm.sendRequest('https://postman-echo.com/get', (error, response) => {

        if (error) {

            console.log(error);

        } else {

            console.log(response);

        }

    });

    await sleep(500)

    console.info("after: " + i)

}

})()

function sleep(ms) {

    return new Promise(resolve => setTimeout(resolve, ms));

}

setTimeout(()=>{},15000)

Add you full code inside the async function and add await sleep after each pm.sendRequest.

This approach will just slow down your test execution

3 Likes