Conditional Importing In JavaScript

JavaScript

05/08/2021


To conditionally import a module or whatnot, you got to make use of dynamic imports. As an example:

JAVASCRIPT
// In async function
const module = await import('./someModule')

Pay attention to the import keyword. If used as a function, it returns a promise.

Knowing that, conditionally importing is then a walk in the park. 🌳

JAVASCRIPT
// In async function
let someModule
if (someCondition) someModule = await import('./someModule')

WRITTEN BY

Code and stuff