Skip to content

Instantly share code, notes, and snippets.

@brianswisher
Created July 16, 2019 00:18
Show Gist options
  • Save brianswisher/90dcf6814b76521465988cf3f567a479 to your computer and use it in GitHub Desktop.
Save brianswisher/90dcf6814b76521465988cf3f567a479 to your computer and use it in GitHub Desktop.
Using dynamic require with JavaScript
((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)
})
}
})
})
@brianswisher
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment