Throw new Error is not throwing in async function

I have a pre request script below in the collection. I create a get request in the collection and send it. The request still send without throwing any error. Is it a bug? I don’t have this issue when I used a old version with scratchpad.

console.log('test1');


(async function main() {
    console.log('test2');
    throw new Error("error 1");
    console.log('test3');
})();

Version 10.18.9
UI Version: 10.18.9-ui-230926-0710
Desktop Platform Version: 10.18.7 (10.18.7)

Hey @telecoms-physicist11 :wave:

What is the overall goal of what you’re trying to do here?

Can you explain the use case and why using an approach like this is required?

@dannydainton
I have multiple pm.sendRequest() and the result is depends on one by one

Psuedo Code:

result1 = sendRequest(url1);
result2 = sendRequest(url2, result1);
result3 = sendRequest(url3, result2);
result4 = sendRequest(url4, result3);

However, it will cause a callback hell problem if I use pm.sendRequest() directly. So, I promisefy pm.sendRequest() as below

const sendRequest = (req) => {
    return new Promise((resolve, reject) => {
        pm.sendRequest(req, (err, res) => {
            if (err) {
                console.log(err);
                throw new Error(`Error occurs for request ${req.url}`);
            }

            resolve(res);
        });
    });
};

So, I can use it in async like below. The construction of request body is simplified below for lucidity.

(async function main() {
    const result1 = await sendRequest(req1);

    req2.body.result = result1;
    const result2 = await sendRequest(req2);

    req3.body.result = result2;
    const result3 = await sendRequest(req3);

    req4.body.result = result3;
    const result4 = await sendRequest(req4);

    pm.variables.set("result4", result4);
})();

Also, it works for the old version when I use scratchpad in postman. Error will thrown and the request will not start in the collections. After update to the new version, this behaviour disappear.

@danny-dainton May I know if you have any updates for this?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.