Created
July 16, 2019 00:18
-
-
Save brianswisher/90dcf6814b76521465988cf3f567a479 to your computer and use it in GitHub Desktop.
Using dynamic require with JavaScript
This file contains 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
((name)=>{ | |
return fetch(`https://wzrd.in/bundle/${name}@latest/`) | |
.then(response => response.text()) | |
.then(body => { | |
eval(body) | |
window[name] = require(name) | |
}) | |
})('jquery').then(()=>{ | |
const $ = jquery | |
const getUserRank = user => { | |
const { id, name, email } = user | |
return `${name}: ${id + name.length + email.length}` | |
} | |
$.ajax({ | |
url: 'https://jsonplaceholder.typicode.com/users', | |
type: 'GET', | |
data: {}, | |
success: (users) => { | |
$.each(users, (index, user) => { | |
setTimeout(() => console.log( getUserRank(user) ), 1000 * index) | |
}) | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://youtu.be/LNqTRs7JJ2Q