Master de II. ULL. 1er cuatrimestre. 2020/2021
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p id="out"></p>
<script>
'use strict';
let script = document.createElement('script');
document.head.append(script);
let out = document.getElementById('out');
function loadScript(src) {
return new Promise(function(resolve, reject) {
script.src = src;
script.onload = (e) => resolve(script);
script.onerror = (Err) => reject(new Error(`Script load error for ${src}`));
});
}
let promise = loadScript("https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js");
promise.then(
script => {
out.innerHTML = `<br/>Promise solved: script ${script.src} is loaded!`;
console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
}
).catch(
error => {
out.innerHTML += `<br/>Promise rejected: Error!<br/> Error Message: ${error.message}`
});
promise.then(script => {
out.innerHTML += '<br/>Another handler for the same promise ... '
console.log(_.times(3, String)); // => ['0', '1', '2']
}).catch(
error => {
out.innerHTML += `<br/>Another Handler!<br/>Promise rejected: Error!<br/> Error Message: ${error.message}`
}
);
</script>
</body>
</html>