---
agentTools:
  projectIndex: https://operations.readme.io/llms.txt
---

# Cancellation

Responding to cancellation

`NSOperation` instances can be cancelled, responding to cancellation effecting will ensure that overall performance of the queue and application is optimal. There are a number of situations which require attention regarding cancellation

[block:api-header]
{
  "type": "basic",
  "title": "Check whether the operation has been cancelled"
}
[/block]

Before doing *work* inside the `Operation` subclass, it is a best practice to check whether the operation has already been cancelled. Let's revisit our first `Operation` subclass, and do it properly :)

[block:code]
{
  "codes": [
    {
      "code": "import Operations\n\nclass MyFirstOperation: Operation {\n    override func execute() {\n        guard !cancelled else { return }\n        print(\"Hello World\")\n        finish()\n    }\n}",
      "language": "swift",
      "name": "check cancelled property before doing work"
    }
  ]
}
[/block]