> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/advanced/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/advanced/3.4.-the-chain-of-responsibility-pattern-ok.md).

# 3.4. The Chain of Responsibility Pattern (ok)

![](/files/-MTRLeTlHfkAmSONCxqt)

C:\xampp\htdocs\php\js\main.js

```
require(['cor/init'],function(init) {
	var examples = {
		cor: init
	}
	for (var example in examples) {
		examples[example].init();
	}
});
```

C:\xampp\htdocs\php\js\cor\init.js

```
define(function(require) {
  'use strict';
  return {
    init: function() {
      var call, sms, email, handler, telepathy, Handler = require('cor/handler');
      var callHandler = require('cor/handlers/call');
      call = {
        type: 'call',
        number: '07123456789',
        ownNumber: '070031101003'
      };
      sms = {
        type: 'sms',
        number: '07123456789',
        message: 'Hey Dan'
      };
      email = {
        type: 'email',
        recipient: 'dan@danwellman.co.uk',
        message: 'Hi Dan'
      };
      telepathy = {
        type: 'esp',
        target: 'someone else',
        message: 'spooky'
      };
      handler = new Handler(null, null, callHandler);
      handler.handleCommunication(email);
      handler.handleCommunication(sms);
      handler.handleCommunication(call);
      handler.handleCommunication(telepathy);
    }
  };
});
```

C:\xampp\htdocs\php\js\cor\handler.js

```
define(function() {
  'use strict';
  var CommunicationHandler = function(communicationType, handler, nextHandler) {
    this.communicationType = communicationType;
    this.handler = handler;
    this.nextHandler = nextHandler;
  };
  CommunicationHandler.prototype.handleCommunication = function(communication) {
    if (communication.type !== this.communicationType) {
      (this.nextHandler) ? this.nextHandler.handleCommunication(communication): console.log('Communication type', communication.type, 'could not be handled');
      return;
    }
    this.handler(communication);
  };
  return CommunicationHandler;
});
```

C:\xampp\htdocs\php\js\cor\handlers\call.js

```
define(function(require) {
  'use strict';
  var Handler = require('cor/handler');
  var smsHandler = require('cor/handlers/sms');
  var callHandler;
  callHandler = new Handler('call', handleCall, smsHandler);
  function handleCall(call) {
    console.log('Call placed to number', call.number, 'from number', call.ownNumber);
  }
  return callHandler;
});
```

C:\xampp\htdocs\php\js\cor\handlers\sms.js

```
define(function(require) {
  'use strict';
  var Handler = require('cor/handler');
  var emailHandler = require('cor/handlers/email');
  var smsHandler;
  smsHandler = new Handler('sms', handleSms, emailHandler);
  function handleSms(sms) {
    console.log('SMS sent to number', sms.number, 'message: ', sms.message);
  }
  return smsHandler;
});
```

C:\xampp\htdocs\php\js\cor\handlers\email.js

```
define(function(require) {
  'use strict';
  var Handler = require('cor/handler');
  var emailHandler;
  emailHandler = new Handler('email', handleEmail, null);
  function handleEmail(email) {
    console.log('Email sent to', email.recipient, 'message: ', email.message);
  }
  return emailHandler;
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/advanced/3.4.-the-chain-of-responsibility-pattern-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
