This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function loadScript(src) { | |
| // creates a <script> tag and append it to the page | |
| // this causes the script with given src to start loading and run when complete | |
| let script = document.createElement('script'); | |
| script.src = src; | |
| document.head.append(script); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // load and execute the script at the given path | |
| loadScript('/my/script.js'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| loadScript('/my/script.js'); // the script has "function newFunction() {…}" | |
| // the code below loadScript | |
| // doesn't wait for the script loading to finish | |
| // ... | |
| newFunction(); // no such function! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // define function loadScript | |
| function loadScript(src, callback) { | |
| let script = document.createElement('script'); | |
| script.src = src; | |
| script.onload = () => callback(script); | |
| document.head.append(script); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| loadScript('/my/script.js', function(script) { | |
| loadScript('/my/script2.js', function(script) { | |
| loadScript('/my/script3.js', function(script) { | |
| // ...continue after all scripts are loaded | |
| }); | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // DEFINE function in Promise way | |
| function loadScript(src) { | |
| return new Promise(function(resolve, reject) { | |
| let script = document.createElement('script'); | |
| script.src = src; | |
| script.onload = () => resolve(script); | |
| script.onerror = () => reject(new Error(`Script load error for ${src}`)); | |
| document.head.append(script); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function printString(string, callback) { | |
| setTimeout( | |
| () => { | |
| console.log(string) | |
| callback() | |
| }, | |
| 1 * 1000 // 1s | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function printAll() { | |
| printString("A", () => { | |
| printString("B", () => { | |
| printString("C", () => {}) | |
| }) | |
| }) | |
| } | |
| printAll() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function printString(string) { | |
| return new Promise((resolve, reject) => { | |
| setTimeout( | |
| () => { | |
| console.log(string) | |
| resolve() | |
| }, | |
| 1 * 1000 | |
| ) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function printAll() { | |
| printString("A") | |
| .then(() => { | |
| return printString("B") | |
| }) | |
| .then(() => { | |
| return printString("C") | |
| }) | |
| } |