# Async\\Await within pm.test

**URL:** <https://community.postman.com/t/async-await-within-pm-test/65070>\
**Category:** Help Hub\
**Tags:** tests, async\
**Created:** [August 5, 2024, 12:28pm UTC](https://community.postman.com/t/async-await-within-pm-test/65070 "2024-08-05T12:28:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 5, 2024, 12:28pm UTC](https://community.postman.com/t/async-await-within-pm-test/65070/1 "2024-08-05T12:28:25Z")

</div>

Considering the following example.

```auto
for (let i = 1; i < 11; i++) {

    const response = await pm.sendRequest(`https://postman-echo.com/get?test=request${i}`);

    pm.test(`Request ${i} - status code is 200`, () => {
        pm.expect(response).to.have.status(200);
        let resJson = response.json();
        console.log(resJson.args.test);
    });

};

```

pm.sendRequest now supports top-level awaits. So this works fine as the sendRequest is outside of any functions, and the sendRequests all come out in the correct order.

This would be a typical scenario and this works fine.

However, I would like to move the sendRequest so its within the test block.

The scenario behind this is to run multiple sendRequests() in order, but to stop processing the script if the test related to the first request fails.

I don’t think the pm.test function is async, so I’m wondering if there is a way to get around this in JavaScript.

If you do the following, you get a syntax error.

```auto
for (let i = 1; i < 11; i++) {

    pm.test(`Request ${i} - status code is 200`, () => {
        const response = await pm.sendRequest(`https://postman-echo.com/get?test=request${i}`);
        pm.expect(response).to.have.status(200);
        let resJson = response.json();
        console.log(resJson.args.test);
    });

};

```

**SyntaxError** : _await is only valid in async functions and the top level bodies of modules_

If you do the following, it removes the error, but the requests are no longer in order.

```auto
for (let i = 1; i < 11; i++) {

    pm.test(`Request ${i} - status code is 200`, async () => {
        const response = await pm.sendRequest(`https://postman-echo.com/get?test=request${i}`);
        pm.expect(response).to.have.status(200);
        let resJson = response.json();
        console.log(resJson.args.test);
    });

};

```

What I’m really trying to do is to prevent parsing a response when there is an error.

This also works in a fashion.

```auto
async function sendRequest(request, i) {
    try {
        const response = await pm.sendRequest(request);
        pm.test(`Request ${i} - status code is 200`, () => {
            pm.expect(response).to.have.status(200);
        });
        return {
            'data': response,
            'status': "success"
        };
    } catch (error) {
        console.log(error);
        pm.test(`Request ${i} - status code is 200`, () => {
            pm.expect(error, error.code).to.be.undefined;
        });
        return {
            "status": "error",
            "errorcode": error.code
        }
    }
};

for (let i = 1; i < 11; i++) {
    let response = await sendRequest(`https://postman-echo.com/get?test=request${i}`, i);
    if (response.status === "success") {
        console.log(response.data.json().args.test); // Postman Echo Response
    }
};

```

Posing the question here first before I post the same on StackOverflow.

@danny-dainton Perhaps one for Udit to have a look at if he has the time.

I either need to get access to the error variable that you could get when you send a normal sendRequest or I would like to run the await version within the test function so I can deal with errors in a more refined manner.

---

<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:** [September 4, 2024, 12:28pm UTC](https://community.postman.com/t/async-await-within-pm-test/65070/2 "2024-09-04T12:28:44Z")

</div>

Closed on September 4, 2024, 12:28pm UTC

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