Skip to content

Commit

Permalink
session 27: async, await, promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeeppy committed Apr 26, 2023
1 parent 4eff5b8 commit 5078b82
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions session27/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions session27/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

function chargerScript(script) {
return new Promise((resolve, reject) => {
let element = document.createElement('script');
element.src = script;
document.head.append(element);

element.onload = () => resolve('Fichier ' + script + ' a été chargé');

element.onerror = () => reject(new Error('Opération impossible pour le script ' + script));
});
}

async function resultat() {
try {
const scriptA = await chargerScript('test.js');
console.log(scriptA);
const scriptB = await chargerScript('autre.js');
console.log(scriptB);
}
catch(error) {
console.log(error);
document.head.lastChild.remove();
}
}

resultat();

// async function direBonjour() {
// const promesse = new Promise((resolve, reject) => {
// setTimeout(() => resolve('Promesse tenu !'), 3000);
// });

// let resultat = await promesse;
// console.log(resultat);
// }


// direBonjour();
Empty file added session27/test.js
Empty file.

0 comments on commit 5078b82

Please sign in to comment.