# Throw new Error is not throwing in async function

**URL:** <https://community.postman.com/t/throw-new-error-is-not-throwing-in-async-function/52870>\
**Category:** Help Hub\
**Tags:** scripts, pre-request-script\
**Created:** [October 5, 2023, 12:56pm UTC](https://community.postman.com/t/throw-new-error-is-not-throwing-in-async-function/52870 "2023-10-05T12:56:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![telecoms-physicist11](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@telecoms-physicist11](https://community.postman.com/u/telecoms-physicist11)\
**Post date:** [October 5, 2023, 12:56pm UTC](https://community.postman.com/t/throw-new-error-is-not-throwing-in-async-function/52870/1 "2023-10-05T12:56:05Z")

</div>

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.

```js
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)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 6, 2023, 11:07am UTC](https://community.postman.com/t/throw-new-error-is-not-throwing-in-async-function/52870/2 "2023-10-06T11:07:30Z")

</div>

Hey @telecoms-physicist11 👋

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?

---

<div class="post-metadata">

**Author:** ![telecoms-physicist11](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@telecoms-physicist11](https://community.postman.com/u/telecoms-physicist11)\
**Post date:** [October 6, 2023, 2:32pm UTC](https://community.postman.com/t/throw-new-error-is-not-throwing-in-async-function/52870/3 "2023-10-06T14:32:40Z")

</div>

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

Psuedo Code:

```auto
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

```js
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.

```js
(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.

---

<div class="post-metadata">

**Author:** ![telecoms-physicist11](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@telecoms-physicist11](https://community.postman.com/u/telecoms-physicist11)\
**Post date:** [October 13, 2023, 7:31am UTC](https://community.postman.com/t/throw-new-error-is-not-throwing-in-async-function/52870/4 "2023-10-13T07:31:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [November 12, 2023, 7:31am UTC](https://community.postman.com/t/throw-new-error-is-not-throwing-in-async-function/52870/5 "2023-11-12T07:31:21Z")

</div>

Closed on November 12, 2023, 7:31am UTC

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