# Use await(async) methods inside BatchJob

**URL:** <https://discuss.hangfire.io/t/use-await-async-methods-inside-batchjob/7839>\
**Category:** bug?\
**Tags:** aspnetcore\
**Created:** [October 2, 2020, 6:33am UTC](https://discuss.hangfire.io/t/use-await-async-methods-inside-batchjob/7839 "2020-10-02T06:33:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajitha\_Kithuldeniya](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/rajitha_kithuldeniya/32/2261_2.png) [@Rajitha\_Kithuldeniya](https://discuss.hangfire.io/u/Rajitha_Kithuldeniya)\
**Post date:** [October 2, 2020, 6:33am UTC](https://discuss.hangfire.io/t/use-await-async-methods-inside-batchjob/7839/1 "2020-10-02T06:33:16Z")

</div>

```
BatchJob.StartNew(async(batchJob) => {
     for (var i = 0; i < 10; i++)
    {
        x.Enqueue(() => SendEmail(i));

       await doSomework(i)
    }
}

```

If i call async method inside batchjob only back ground job will be created.

```
BatchJob.StartNew((batchJob) => {
     for (var i = 0; i < 10; i++)
    {
        x.Enqueue(() => SendEmail(i));

       doSomework(i).wait()
    }
}

```

but when i use .wait() method it is working.

---

<div class="post-metadata">

**Author:** ![Rajitha\_Kithuldeniya](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/rajitha_kithuldeniya/32/2261_2.png) [@Rajitha\_Kithuldeniya](https://discuss.hangfire.io/u/Rajitha_Kithuldeniya)\
**Post date:** [October 13, 2020, 12:46pm UTC](https://discuss.hangfire.io/t/use-await-async-methods-inside-batchjob/7839/2 "2020-10-13T12:46:44Z")

</div>

I mean only **One back ground** job will be created

---

<div class="post-metadata">

**Author:** ![jane\_doe](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/jane_doe/32/2874_2.png) [@jane\_doe](https://discuss.hangfire.io/u/jane_doe)\
**Post date:** [April 8, 2022, 8:49am UTC](https://discuss.hangfire.io/t/use-await-async-methods-inside-batchjob/7839/3 "2022-04-08T08:49:27Z")

</div>

I know it’s late but i answer for further knowledge:

The problem is that that BatchJob.StartNew(…) accept an Action as parameter (not a Function).  
An async Action is not awaited, so the startNew Task end after firing the async Action (async(batchJob)).

The second code is working not because you use .wait(); instead of await but because the main action is sincronous.

J-
