-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.js
176 lines (144 loc) · 4.75 KB
/
async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
(function () {
var request = new XMLHttpRequest();
request.open("GET", "data/movies.json", true);
request.onload = function(){
if(request.readyState === XMLHttpRequest.DONE && request.status === 200){
// console.log(JSON.parse(http.responseText));
var response = JSON.parse(request.responseText);
// console.log(response);
console.table(response.movies);
var movies = response.movies;
var output = '';
for (var i = 0; i < movies.length; i++) {
output += '<li>' + movies[i].title + '</li>';
}
document.getElementById("p").innerHTML = output;
} else {
console.error("Something went wrong.");
}
};
request.send();
// POST info
var url = "http://jsonplaceholder.typicode.com/posts";
var data = {
title: "Ferrari",
body: "Enzo"
};
// data.title = "Ferrari";
// data.body = "Enzo";
var json = JSON.stringify(data);
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.onload = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 201) {
var info = JSON.parse(xhr.responseText);
//console.log(info);
console.table(info);
} else {
console.error("Something went wrong.");
}
};
xhr.send(json);
// PUT to update info
var url = "http://jsonplaceholder.typicode.com/posts/1";
var data = {
title: "Porsche",
body: "Carrera"
};
// data.title = "Porsche";
// data.body = "Carrera";
var json = JSON.stringify(data);
var xhr = new XMLHttpRequest();
xhr.open("PUT", url, true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.onload = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
var info = JSON.parse(xhr.responseText);
//console.log(info);
console.table(info);
} else {
console.error("Something went wrong.");
}
};
xhr.send(json);
// DELETE info
var url = "http://jsonplaceholder.typicode.com/posts/1";
var xhr = new XMLHttpRequest();
xhr.open("DELETE", url, true);
xhr.onload = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
var info = JSON.parse(xhr.responseText);
console.log(info);
console.table(info);
} else {
console.error("Something went wrong.");
}
};
xhr.send(null);
// jQuery
$.getJSON("data/players.json", data => {
//console.log(players);
const players = data.players;
let output = '';
for (let i = 0; i < players.length; i++) {
output += '<li>' + players[i].name + '</li>';
}
$("#p").html(output);
});
//Promises
$.get("data/clubs.json")
.then(clubs => {
console.log(clubs);
return $.get("data/movies.json");
})
.then(movies => {
console.log(movies);
return $.get("data/players.json");
})
.then(players => console.log(players))
.catch(errors => console.log(errors.responseText));
const info = async () =>{
const clubs = await $.get("data/clubs.json");
const movies = await $.get("data/movies.json");
const players = await $.get("data/players.json");
return [clubs, movies, players];
};
info().then(res => console.log(res));
Promise.all([
$.get("data/clubs.json"),
$.get("data/movies.json"),
$.get("data/players.json")
]).then(([clubs, movies, players]) => {
console.log(clubs, movies, players);
});
var getData = async () => {
return await Promise.all([
$.getJSON("data/clubs.json"),
$.getJSON("data/movies.json"),
$.getJSON("data/players.json")
]);
};
getData().then(res => console.log(res));
//Generators
genWrap(function*() {
const players = yield $.getJSON("data/players.json");
console.log(players);
const clubs = yield $.getJSON("data/clubs.json");
console.log(clubs);
const movies = yield $.getJSON("data/movies.json");
console.log(movies);
});
function genWrap(generator){
// prepare the generator
let g = generator();
function handle(yielded){
if(!yielded.done){
yielded.value.then( data => {
return handle(g.next(data));
})
}
}
return handle(g.next());
}
})();