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

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!

1 Like

@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:

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
  } catch {
    result = .failure(error)
  await onTaskCompleted()
  return result

PR: fixes a bugfix by icanzilb · Pull Request #20 · raywenderlich/mcon-materials · GitHub