forked from jaandrle/CordovaTips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromises.js
136 lines (123 loc) · 4.28 KB
/
Promises.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
/*
* Par ukazek pro objekt Promises
* */
// Klasicky sleep, tj. kod "v then" se provede po prodleve
function sleep(prodleva) {
return new Promise(function(resolve) {
console.log(this); //this is corresponding to window
window.setTimeout(function() {
resolve();
}, prodleva);
});
};
console.log("prvni vypis");
var milliseconds = 2000;
sleep(milliseconds).then(console.log("druhy vypis"));
console.log("treti vypis");
// Kod "v then" se provede po kompletnim nacteni stranky
function DOMready() {
// Credit to Jake Archibald
// https://github.com/jakearchibald/svgomg/blob/master/src/js/page/utils.js#L7
return new Promise(function(resolve) {
function checkState() {
if (document.readyState !== 'loading') resolve();
}
document.addEventListener('readystatechange', checkState);
checkState();
});
};
console.log("prvni vypis");
DOMready().then(console.log("druhy vypis"));
console.log("treti vypis");
// Fce .map a .all
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
// .map: paralerni volani jedne casti kodu bez zachovani poradi
window.addEventListener('WebComponentsReady', function(){
home= document.querySelector('KAM');
getJSON("FROM_URL")
.then(function(response){
response.results.map(function(url){
getJSON(url).then(createPlanetThumb);
});
})
});
// .all: paralerni volani jedne casti kodu se zachovanim poradi
window.addEventListener('WebComponentsReady', function(){
home= document.querySelector('KAM');
getJSON("FROM_URL")
.then(function(response){
return Promise.all(response.results.map(getJSON));
})
.then(function(arrayOfPlanetData){
arrayOfPlanetData.forEach(function(planet){
createPlanetThumb(planet);
});
})
.catch(function(error){
console.log(error);
});
});
// optimize
/**
* Helper function to create a planet thumbnail - Promisified version!
* @param {Object} data - The raw data describing the planet.
*/
function createPlanetThumb(data) {
return new Promise(function(resolve) {
var pT = document.createElement('planet-thumb');
for (var d in data) {
pT[d] = data[d];
}
home.appendChild(pT);
console.log('rendered: ' + data.pl_name);
resolve();
});
}
/**
* Performs an XHR for a JSON and returns a parsed JSON response - with a delay!
* @param {String} url - The JSON URL to fetch.
* @return {Promise} - A promise that passes the parsed JSON response.
*/
function getJSON(url) {
console.log('sent: ' + url);
return get(url).then(function(response) {
// For testing purposes, I'm making sure that the urls don't return in order
if (url === 'data/planets/Kepler-62f.json') {
return new Promise(function(resolve) {
setTimeout(function() {
console.log('received: ' + url);
resolve(response.json());
}, 500);
});
} else {
console.log('received: ' + url);
return response.json();
}
});
}
window.addEventListener('WebComponentsReady', function() {
home = document.querySelector('section[data-route="home"]');
getJSON('../data/earth-like-results.json')
.then(function(response) {
addSearchHeader(response.query);
return response;
})
.then(function(response) {
var sequence = Promise.resolve();
// .map executes all of the network requests immediately.
var arrayOfExecutingPromises = response.results.map(function(result) {
return getJSON(result);
});
arrayOfExecutingPromises.forEach(function(request) {
// Loop through the pending requests that were returned by .map (and are in order) and
// turn them into a sequence.
// request is a getJSON() that's currently executing.
sequence = sequence.then(function() {
// Remember that createPlanetThumb is a Promise, so it must resolve before Promises
// later in the sequence can execute.
return request.then(createPlanetThumb);
});
});
});
});