# Chapter 7 - Small bug in code (failed tasks don't decrement count)

**URL:** <https://forums.kodeco.com/t/chapter-7-small-bug-in-code-failed-tasks-dont-decrement-count/169621>\
**Category:** Modern Concurrency in Swift\
**Created:** [April 22, 2022, 5:25pm UTC](https://forums.kodeco.com/t/chapter-7-small-bug-in-code-failed-tasks-dont-decrement-count/169621 "2022-04-22T17:25:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![banderman](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/banderman/32/102075_2.png) [@banderman](https://forums.kodeco.com/u/banderman)\
**Post date:** [April 22, 2022, 5:25pm UTC](https://forums.kodeco.com/t/chapter-7-small-bug-in-code-failed-tasks-dont-decrement-count/169621/1 "2022-04-22T17:25:01Z")

</div>

Hi! My coworkers and I noticed a small issue in the code in Chapter 7: when a task fails in `worker(number:)`, it returns without calling `await onTaskCompleted()`. On my computer, that results in the scheduled count going from 4 up to 5 when it first encounters a failing task. Obviously not a big deal, but just FYI.

I wonder if there’s a better solution than just copying and pasting that line before the return. I couldn’t think of a good one (`defer` doesn’t seem to work with `await`). Otherwise, interesting chapter!

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [April 24, 2022, 9:57am UTC](https://forums.kodeco.com/t/chapter-7-small-bug-in-code-failed-tasks-dont-decrement-count/169621/2 "2022-04-24T09:57:43Z")

</div>

@banderman Wow, that’s very nicely spotted - it’s such a small detail to notice while running the app that I haven’t noticed it all! Thank you for letting me know.

If you don’t want to repeat that line you can write the function as:

```swift
func worker(number: Int) async -> Result<String, Error> {
await onScheduled()

  let task = ScanTask(input: number)

  let result: Result<String, Error>
  do {
    result = try .success(await task.run())
  } catch {
    result = .failure(error)
  }
  
  await onTaskCompleted()
  return result
}

```

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [April 24, 2022, 10:36am UTC](https://forums.kodeco.com/t/chapter-7-small-bug-in-code-failed-tasks-dont-decrement-count/169621/3 "2022-04-24T10:36:30Z")

</div>

PR: [fixes a bugfix by icanzilb · Pull Request #20 · raywenderlich/mcon-materials · GitHub](https://github.com/raywenderlich/mcon-materials/pull/20)
