Is there a better way, with JavaScript ES6, to load a script and then run a function after that script is ready? Basically loading the script through JavaScript, waiting until it's ready to use, then running a function that might methods etc. from the loaded script.
For instance, a simple way to do this with jQuery is:
$.getScript('foo.js', function() {
runFunction();
});
Is there a reliable alternative to this?
Yes. You should use a Promise . Specific to network related requests, see the Fetch api: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
fetch('http://example.com/movies.json')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.