To dynamically import a module from a CDN, you can use the import()
function, which returns a promise that resolves to the imported module. Here’s an example:
import('https://cdn.example.com/my-module.js')
.then(module => {
// Module has been loaded and can be used here
module.myFunction();
})
.catch(error => {
// Handle any errors that occurred during module loading
console.error(error);
});
In this example, we use the import()
function to import the module located at https://cdn.example.com/my-module.js
. Once the module is loaded, the promise resolves and we can access its exported functions or variables.
If the dynamic import fails, such as if the module URL is incorrect or the network connection is unavailable, the catch
block will execute and you can handle the error accordingly.
Note that dynamic imports are supported in modern browsers but may require a polyfill or transpilation for compatibility with older browsers. Make sure to check the browser compatibility of dynamic imports before using them in production.
For more information, you can refer to the MDN web docs on dynamic import or the ECMAScript specification.