E2020 dynamic imports in JavaScript

Escmascript Is a standard from which JavaScript and typescript are both derived from.

ES2020 is out it comes with it new ways of writing JavaScript code.

Dynamic imports enable importing dependencies in a rather dynamic way.

Initially JavaScript developers were restricted to statically load their dependencies for instance;

import { property } from 'dependency'

On runtime the JavaScript engine loads the required dependencies and makes them accessible globally.

However that is a rigid way of writing code.

Dynamic imports are  relatively more flexible therefore you can included them at any segment within your program.

For instance if you want to load file system module only if a certain criteria is met you would do as follows;

If(process.argv[2] == 'start')
Import fs from 'fs'

Dynamic imports return a promise use the syntax below to make use of them

import('fs')
.then(fs => {
   // consume module fs
})

Comments

Popular posts from this blog

JavaScript introduction to variables

Solutions to blocked URLs on Facebook

how to dump a postgresql database using from the terminal

How to set content disposition headers for express nodejs apps

How to get user's ip address in JavaScript