Skip to content

Commit beeacc8

Browse files
authored
Merge pull request #1 from Asyfundyar/master
Add Async JS, AJAX and Fetch API
2 parents 7f5299d + 0b3c773 commit beeacc8

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
let request = new XMLHttpRequest();
2+
3+
let scriptURL = "http://";
4+
let asynch = true; // setting this as true insures that it will behave asynchronous
5+
6+
request.open("GET", scriptURL, asynch);
7+
8+
/* Setting the function that does something with the request */
9+
request.onreadystatechange = doSomething; // will not wait
10+
11+
/* Sending request */
12+
request.send(null);
13+
14+
function doSomething() {
15+
// something..
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let url = "https://jsonplaceholder.typicode.com/todos/1";
2+
3+
/*
4+
creating a variable for the returned promise
5+
*/
6+
const promise = fetch(url);
7+
8+
/*
9+
Unwrapping the promise with .then
10+
Note: Notice that response.json() also returns a promise
11+
*/
12+
promise.then((response) => response.json()).then((json) => console.log(json));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Asynchronous JavaScript, Ajax and Fetch API
2+
3+
- [Asynchronous JavaScript](#asynchronous-javaScript)
4+
- [Ajax](#ajax)
5+
- [Fetch API](#fetch-api)
6+
7+
## Asynchronous JavaScript
8+
9+
Asychronous tools allow users to continue processing other requests or executing other code without waiting for one request/execution to complete.
10+
11+
Promises, which we will learn more about when using the Fetch API, also provides help when dealing with asynchronous requests.
12+
13+
## Ajax
14+
15+
AJAX stands for Asynchronous JavaScript And XML.
16+
17+
It allows users to accomplish a few things, such as, reading data from a web server after a page has loaded, updating a web page without the need to reload the whole page, and sending back information to a web server asynchronously (in the background).
18+
19+
AJAX uses the XMLHttpRequest object to accomplish these various tasks.
20+
21+
## Fetch API
22+
23+
The Fetch API provides an interface for fetching content or resources across the internet.
24+
25+
It returns a promise, which represent the resulting value of an asynchronous operation in case of a successful and a value that represents the failure of the request.

0 commit comments

Comments
 (0)