-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
97 lines (71 loc) · 2.48 KB
/
script.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
// Using Simple Callback
$.getJSON('http://hipsterjesus.com/api/', function(data) {
$('body').append(data.text);
});
// Using promise returned by $.getJSON
/*var promise = $.getJSON('http://hipsterjesus.com/api/');
promise.then(function(data){
$('body').append(data.text);
}); */
// Pyramid of doom, Always AVOID it
/*$.getJSON('http://hipsterjesus.com/api/', function(data) {
console.info('Inside First Callback');
$.getJSON('http://hipsterjesus.com/api/', function(data) {
console.info('Inside Second Callback');
$.getJSON('http://hipsterjesus.com/api/', function(data) {
console.info('Inside Third Callback');
$.getJSON('http://hipsterjesus.com/api/', function(data) {
console.info('Inside Fourth Callback');
});
});
});
});*/
// Promise inside promise (Can also form Pyramid of DOOM)
/*var promise = $.getJSON('http://hipsterjesus.com/api/');
promise.then(function(data) {
console.info('Inside first promise');
var promise2 = $.getJSON('http://hipsterjesus.com/api/');
promise2.then(function(data){
console.info('Inside Second promise')
var promise3 = $.getJSON('http://hipsterjesus.com/api/');
promise3.then(function(data){
console.info('Inside Third promise')
var promise4 = $.getJSON('http://hipsterjesus.com/api/');
promise4.then(function(data){
console.info('Inside Fourth promise')
});
});
});
}); */
// Promise, By proper way(Chaining)
/*var promise = $.getJSON('http://hipsterjesus.com/api/');
promise.then(function (data) {
console.info('Inside first promise');
return $.getJSON('http://hipsterjesus.com/api/');
}).then(function (data) {
console.info('Inside Second promise');
return $.getJSON('http://hipsterjesus.com/api/');
}).then(function (data) {
console.info('Inside Third promise');
return $.getJSON('http://hipsterjesus.com/api/');
}).then(function (data) {
console.info('Inside Fourth promise');
}).catch(function (error) {
console.warn('There is some error', error);
}); */
// When-then(jQuery) like structure for promise
/*Promise.all([
$.getJSON('http://hipsterjesus.com/api/'),
$.getJSON('http://hipsterjesus.com/api/'),
$.getJSON('http://hipsterjesus.com/api/'),
$.getJSON('http://hipsterjesus.com/api/')
]).then(function(values){
console.warn('Inside First promise');
console.log(values[0])
console.warn('Inside Second promise');
console.log(values[1])
console.warn('Inside Third promise');
console.log(values[2])
console.warn('Inside Fourth promise');
console.log(values[3])
})*/