# Javascript - AJAX
## Lesson Objectives
1. Explain AJAX
1. Demonstrate jQuery AJAX
1. Explain promises
1. Explain creating your own promises
## Explain AJAX
- stands for Asynchronous JavaScript and XML
- No longer uses XML. Uses JSON
- The browser, using JavaScript makes a request without reloading current page
- normally, the user makes a request by clicking a link/typing in a URL
## Demonstrate jQuery AJAX
```html
```
## Explain promises
- a way to organize your code better
- no added functionality, just easier to read
- $.ajax(...) returns a promise object
- this object has functions on it for when the ajax completes, depending on state
- done (successfully completes)
- fail (error encountered)
- always (either error or success)
- pass in a callback function to be executed when the various situations arise
```html
```
- the `done()`, `fail()`, and `always()` functions return the same promise object
- you can use this returned promise object to chain your callbacks onto each other
- done can contain multiple callbacks
```html
```
- use `.then()` as a shorthand function which takes two function callbacks as parameters
- 1st param is the success callback
- 2nd param is the error callback
```html
```
## Explain creating your own promises
- `$.Deferred();` returns a deferred object
- a deferred object can create a promise
- it can also successfully resolve that promise or have it error out
- you can then `resolve()` or `reject()` these promises whenever you want
- use `$.when` to test when multiple promises are resolved
- success when all succeed
- failure when one or more fail
- could do this using nested promises
```html
```